- 保存图片至相册功能需要注意,需要将 NSPhotoLibraryAddUsageDescription 授权请求添加到Info.plist文件中。不然在调用保存图片API时,app会闪退,并报错:
This app has crashed because it attempted to access privacy-sensitive data without a usage description. The app's Info.plist must contain an NSPhotoLibraryAddUsageDescription key with a string value explaining to the user how the app uses this data.
代码:
//保存图片至相册
UIImageWriteToSavedPhotosAlbum(finalImage, self, #selector(self.image(image:didFinishSavingWithError:contextInfo:)), nil)
/// 图片保存成功或失败的回调方法
@objc func image(image:UIImage,didFinishSavingWithError error:NSError?,contextInfo:AnyObject) {
if error != nil {
//...保存图片失败
} else {
//...保存图片成功
}
}