iOS保存图片闪退

问题描述

1.保存图片时调用了
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)方法
2.个别测试机运行时APP闪退,XCode提示崩溃信息如下

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.根据提示加入这个权限就没有问题了,之前加入的有NSPhotoLibraryUsageDescription这个权限,但是个别测试机会报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版本新增的权限,涉及保存图片到相册时需要添加该权限。

  1. 下面是网上搜集的这两个权限的区别
iOS11以前:  
NSPhotoLibraryUsageDescription:访问相册和存储照片到相册(读写),会出现用户授权。 
iOS11之后:  
NSPhotoLibraryUsageDescription:无需添加。默认开启访问相册权限(读),无需用户授权。  
NSPhotoLibraryAddUsageDescription: 添加内容到相册(写),会出现用户授权。 
来源:CSDN博客

你可能感兴趣的:(iOS保存图片闪退)