iOS截屏

作者: 谁在弹奏一曲东风破 | 来源:发表于2016-06-18 18:02 被阅读257次

截屏方法有很多中,这里只写一种最简单的,以及什么样的情况下用什么参数

具体的主要函数如下:

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, 0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

第一个函数:

UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale) 

size 画布的大小
opaque 是否有透明度
scale 画布的比率,这个参数关系到当前所截图片的清晰度一般设置为当前屏幕的比率 [UIScreen mainScreen].scale

如果不关心这些,也可以直接使用

UIGraphicsBeginImageContext(self.view.bounds.size)

但涉及到分享的时候最好使用options函数;

封装了一下,具体写法:

+ (UIImage*)mcScreenShotWithObject:(id)target
                      shotSize:(CGSize)size
                      isOpaque:(BOOL)opaque
                        isSave:(BOOL)isSave
                      saveName:(NSString*)name{
UIGraphicsBeginImageContextWithOptions(size, opaque, [UIScreen mainScreen].scale);
if ([target isKindOfClass:[UIView class]]) {
    UIView *view = (UIView*)target;
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
}else if ([target isKindOfClass:[UIViewController class]]){
    UIViewController *viewController = (UIViewController*)target;
    [viewController.view.layer renderInContext:UIGraphicsGetCurrentContext()];
}else{
    NSAssert([target isKindOfClass:[UIView class]] || [target isKindOfClass:[UIViewController class]], @"数据源有问题,请检查并更新!");
}
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

if (isSave) {
    NSString * filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"DocumentPath"];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
    [fileManager createFileAtPath:[filePath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@.png",(name && ![name isEqualToString:@""])?name:@"image"]] contents:UIImagePNGRepresentation(image) attributes:nil];
    NSLog(@"====>>%@ Path:%@%@",(name && ![name isEqualToString:@""])?name:@"image", filePath,[NSString stringWithFormat:@"/%@.png",(name && ![name isEqualToString:@""])?name:@"image"]);
}

    return image;
}

相关文章

  • flutter:截屏

    1.flutter-截屏组件 2.flutter-截屏插件 3.flutter-iOS原生截屏 iOS代码 4.获...

  • (最新)iOS截屏

    ios webview 截屏:ios截屏 前言:介绍一下截屏有很多种做法1:截当前屏幕内容2:截整个视图的所有内容...

  • ios截屏

    ios截屏

  • iOS 应用内截屏分享

    需求:捕获用户截屏操作,并建议用户截屏后的操作。虽然iOS11 有系统的截屏,但 APP 内截屏可便捷操作。 封装...

  • iOS 截屏&长截屏

    截屏在 iOS 开发中经常用到,本篇文章讲的是监听用户截屏操作,并且获取截屏图片,如果当前是UIScrollVie...

  • iOS屏幕截图功能

    iOS7.0之前的系统,可以通过以下代码实现截屏功能。 iOS7.0之后,系统中封装了截屏的方法- (UIView...

  • iOS截屏

    1. 一句代码截屏 2. UIGraphics 3. 还有就是之前一个大佬写的给webview截长图的 其实就是利...

  • iOS 截屏

    最后调用:UIImage *capturedImage = [img captureView]; 注释的是把.m中...

  • IOS 截屏

  • iOS截屏

    现在我们只需要一行代码就可以完成上述步骤

网友评论

      本文标题:iOS截屏

      本文链接:https://www.haomeiwen.com/subject/yyfbdttx.html