iOS11 拍照闪退,保存图片闪退问题(解决)

问题描述

最近项目出现一个bug,升级iOS11系统的手机使用我们app保存图片到相册或者拍照会闪退。
经过一番查找,发现是下面代码导致的:
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

问题解决

  1. 一开始以为是该函数的调用方法出现了变化,但是在不同的地方进行调用,或者传入不同的参数,都仍然会闪退;
  2. 进行一番调试,发现crash时候会出现这样一个之前闪退时未出现的错误信息:

    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.

  3. 根据错误提示,在plist文件中增加了NSPhotoLibraryAddUsageDescription属性后,不再出现闪退问题。

问题原因


  1. 解决问题后,我查找了一下苹果开发文档,官方文档是这样描述的:

NSPhotoLibraryAddUsageDescription
NSPhotoLibraryAddUsageDescription (String - iOS) This key lets you describe the reason your app seeks write-only access to the user’s photo library. When the system prompts the user to allow access, this string is displayed as part of the alert.
Important: To protect user privacy, an iOS app linked on or after iOS 10.0, and that accesses the user’s photo library, must statically declare the intent to do so. Include the NSPhotoLibraryAddUsageDescription key (in apps that link on or after iOS 11) or NSPhotoLibraryUsageDescription key in your app’s Info.plist file and provide a purpose string for the key. If your app attempts to access the user’s photo library without a corresponding purpose string, your app exits.
This key is supported in iOS 11.0 and later.
来源:官方文档

这是一个iOS11版本新增的权限,涉及保存图片到相册时需要添加该权限。
2. 由于官方的解释较少,又在网上查找了下其他信息:

iOS11以前:
NSPhotoLibraryUsageDescription:访问相册和存储照片到相册(读写),会出现用户授权。
iOS11之后:
NSPhotoLibraryUsageDescription:无需添加。默认开启访问相册权限(读),无需用户授权。

NSPhotoLibraryAddUsageDescription: 添加内容到相册(写),会出现用户授权。

猜你喜欢

转载自blog.csdn.net/box_kun/article/details/79151888