iOS14以後,儲存圖檔的正确姿勢
如果你在PHAuthorizationStatusLimited 的權限下,儲存圖檔遇到了問題,請參考下面的代碼
// 儲存圖檔到相冊
if (@available(iOS 14,*)) {
WeakSelf
[[PHPhotoLibrary sharedPhotoLibrary]performChanges:^{
[PHAssetChangeRequest creationRequestForAssetFromImage:image];
} completionHandler:^(BOOL success, NSError * _Nullable error) {
dispatch_sync(dispatch_get_main_queue(), ^{
StrongSelf
if (error != nil){
[MBProgressHUD showMessage:@"二維碼儲存失敗" inView:KKeyWindow];
}else{
[MBProgressHUD showMessage:@"二維碼儲存成功" inView:KKeyWindow];
}
[strongSelf dismiss];
});
}];
}else {
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
if (error) {
// 儲存失敗
[MBProgressHUD showMessage:@"二維碼儲存失敗" inView:self.invisibleAutoSave == YES ? KKeyWindow : self];
} else {
// 儲存成功
[MBProgressHUD showMessage:@"二維碼儲存成功" inView:self.invisibleAutoSave == YES ? KKeyWindow : self];
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self dismiss];
});
}