1. 程式人生 > >長按圖片儲存圖片到相簿

長按圖片儲存圖片到相簿

建立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 是否允許使用相機