创建imageView的时候在imageView上添加一个长按手势
UILongPressGestureRecognizer * pressGesture = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(pressGestureImageView:)];
[imageView addGestureRecognizer:pressGesture];
实现长按手势的响应事件,将图片保存到相册
- (void)pressGestureImageView:(UILongPressGestureRecognizer *)pressGesture
{
UIGraphicsBeginImageContextWithOptions(imageView.bounds.size, NO, 0);
CGContextRef ctx = UIGraphicsGetCurrentContext();
[imageView.layer renderInContext:ctx];
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
UIImageWriteToSavedPhotosAlbum(newImage, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if(error != NULL){
popMessageShow(@"保存图片失败", self.view);
}else{
popMessageShow(@"保存图片成功", self.view);
}
NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo);
}
需要注意的是,ios10 之后访问相册等系统工具,需要设置访问权限,不设置的话,访问这些系统工具的时候app会崩溃
info.plist 里添加键值对
Privacy - Photo Library Usage Description 是否允许访问相册
亲测,虽然将图片保存到系统相册并没有使用到 相机,只配置了 相册的权限,使用,调试的时候并不会崩溃,也不报错;但是再往itunes上上传应用的时候,提示你上传成功,但是可构建版本里找不到这个版本。
多次上传,苹果会给你发一封邮件
Dear developer,
We have discovered one or more issues with your recent delivery for “xxxx”. To process your delivery, the following issues must be corrected:
Missing Info.plist key - This app attempts to access privacy-sensitive data without a usage description. The app’s Info.plist must contain an NSCameraUsageDescription key with a string value explaining to the user how the app uses this data.
Once these issues have been corrected, you can then redeliver the corrected binary.
Regards,
The App Store team
告诉我们,仍然需要配置 相机的访问权限
info.plist 里添加相机的访问权限后就可以上传成功了。
Privacy - Camera Usage Description 是否允许使用相机