1. 程式人生 > >iOS11拍照閃退,儲存圖片閃退問題

iOS11拍照閃退,儲存圖片閃退問題

問題描述

最近測試人員反饋,升級iOS11系統的手機使用我們app的拍照功能時閃退。
經過一番查詢,發現是下面程式碼導致的:
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

問題解決

  1. 一開始以為是該函式的呼叫方法出現了變化,但是在不同的地方進行呼叫,或者傳入不同的引數,都仍然會閃退;
  2. 後來使用乾淨的demo工程進行除錯,發現閃退時會出現這樣一個之前閃退時未出現的錯誤資訊:

    [access] 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: 新增內容到相簿(寫),會出現使用者授權。
來源:csdn部落格