iOS 10 App Crashes When Trying to Save Image to Photo Library

iOS 10 Save to Camera Roll Bug/Crash

Add Below permission in Info.plist

<key>NSCameraUsageDescription</key>
<string>This app needs access to the camera to take photos.</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>This app needs access to photos.</string>

More info Click here
Sample Image

iOS10 UIImageWriteToSavedPhotosAlbum TCC__CRASHING_DUE_TO_PRIVACY_VIOLATION

You can save any UIImage to the photo album but first you must ask the user for permission to do as this as it is indeed a privacy issue. If they don't give you access then you can't save the image at all.

The most sensible approach is to add the required privacy key to the info.plist.

This is the info.plist xml definition although it's easier to add the keys in the property list:

<key>NSPhotoLibraryAddUsageDescription</key>
<string>Message requesting the ability to add to the photo library</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Message requestion the ability to access the photo library</string>

If you add these then when you first try to access or add to the photo library a popup will display with your message allowing the user to decide if they want your app to have access.

One good reason to put it in the info.plist file is that all the requests for access are then in a single easily visible place instead of somewhere random in your project.

EDIT

Here is how to save the image in documents which does not raise and privacy issues:

NSData *imageData = UIImagePNGRepresentation(image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"image.png"]; //Add the file name
[imageData writeToFile:filePath atomically:YES]; //Write the file

If you want a jpg instead of a png use this instead:

NSData *imageData = UIImageJPEGRepresentation(image, 0.9); // Use whatever compression ratio you want instead of 0.9

UIImagePickerController crashes app | Swift3, Xcode8

Add this key to your info.plist,

Key : Privacy - Photo Library Usage Description [ NSPhotoLibraryUsageDescription ]
String Value : We need access to your camera roll and photo library, so that we can do operations on it. [ Customise it in your own way]

That's it, Clean & Run the project.

Sample Image

app crashes reported due to missing NSPhotoLibraryAddUsageDescription, but my app never uses any photos

I was able to reproduce the crash! Here's the deal: my app contains a web view. If the user presses on an image in that web view, a Save menu item appears. If the user taps Save, we need permission; if there's no privacy string in the Info.plist, we crash at that moment (and if there is one, we get the authorization dialog).

In my opinion, this is an iOS bug. I never intended for the user to do this; it's just a feature of web views. The act of saving from the web view to the photos library is therefore effectively out of process; it's the user who is doing it, not my app. And the user should not need permission; it's the user who tapped on the image and who then tapped the Save button. There is no way this scenario could have happened without the user making multiple deliberate moves, none of which involve my app directly. I'll be reporting this to Apple.

Saving photo to library more than once using ALAssetsLibrary causes crash

As you already suspect (well done), the problem is this line:

CGImageRelease([self.image CGImage])

You are required to release a CG-type object if and only if you called a Create or Copy method to obtain it. For example, you might have obtained this CGImage by calling the function CGImageCreate(). You didn't do anything like that here, so this is not your object to release. So simply cut those lines, stop doing incorrect memory management, and all will be well.



Related Topics



Leave a reply



Submit