IOS view截图并保存到系统相册

来源:互联网 发布:易语言3d游戏源码 编辑:程序博客网 时间:2024/06/07 10:29

1.声明相册权限

  在info.plist文件中添加

    <key>NSPhotoLibraryUsageDescription</key>

    <string>用于选取照片</string>

  

2.把view转成UIImage

-(UIImage*)imageFromView:(UIView*)view{            CGSize s = view.bounds.size;        // 下面方法,第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果,需要传NO,否则传YES。第三个参数就是屏幕密度了,关键就是第三个参数。        UIGraphicsBeginImageContextWithOptions(s, NO, [UIScreen mainScreen].scale);        [view.layer renderInContext:UIGraphicsGetCurrentContext()];        UIImage*image = UIGraphicsGetImageFromCurrentImageContext();        UIGraphicsEndImageContext();        return image;        }

3.把UIImage保存到系统相册

- (void)saveImageToPhotos:(UIImage*)savedImage{       UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);    }- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{        if (error == nil) {                NSLog(@"存入手机相册成功");            }else{            NSLog(@"存入手机相册失败");    }    }
源码链接


1 0
原创粉丝点击