Error while saving JPG/PNG photo to gallery in IOS 11

848 Views Asked by At

I was testing our application on new device with beta iOS 11 and found an intresting error. We are creating an image which can be saved to user gallery, using UIActivityViewController (some system share view controller):

NSMutableArray *sharingItems = [NSMutableArray new];
UIImage *screenShot = [self shareViewScreenShot];
NSString *filename = @"notImportant";
NSString *path = [NSString stringWithFormat:@"%@%@.jpg", NSTemporaryDirectory(), filename];
[UIImageJPEGRepresentation(screenShot, 1.0) writeToFile:path atomically:YES];
NSURL *pathUrl = [NSURL fileURLWithPath:path];
[sharingItems addObject:pathUrl];

UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:sharingItems applicationActivities:nil];
[self presentViewController:activityController animated:YES completion:nil];

share dialog

This works fine, presenting share dialog, until user press "save image". Then I got error about not suporting this image format

2017-07-19 10:51:54.292881+0200 appName[6632:766119] Video /private/var/mobile/Containers/Data/Application/29B8C33B-9247-45FE-B51F-A35A035019A9/tmp/notImportant.jpg cannot be saved to the saved photos album: Error Domain=AVFoundationErrorDomain Code=-11828 "Nie można otworzyć" UserInfo={NSUnderlyingError=0x1c08558d0 {Error Domain=NSOSStatusErrorDomain Code=-12847 "(null)"}, NSLocalizedFailureReason=Ten format multimediów nie jest obsługiwany., NSURL=file:///private/var/mobile/Containers/Data/Application/29B8C33B-9247-45FE-B51F-A35A035019A9/tmp/notImportant.jpg, NSLocalizedDescription=Nie można otworzyć}

The same happens when I use png/UIImagePNGRepresentation. So the questions are:

  1. Are png/jpg supported in iOS 11?

  2. Should I use/and how HEIF (new apple image format) for devices with os version >= 11?

2

There are 2 best solutions below

0
On

NSOSStatusErrorDomain code means the property data size was not correct.

OSStatus is a type commonly used for error codes in OS X and iOS. If the magnitude of the code is less than 1 million, then the code is probably listed in MacErrors.h in the CarbonCore framework. Otherwise, it is probably a four-character code listed in the same header as the function which returned it. You can find the header of a function by command-clicking it in Xcode. The codes will most likely be listed near the top, grouped together. To convert the number to a code, use the Calculator app in Developer view to convert it to hexadecimal and convert each byte to a character.

0
On

iOS treates image as video? Is this a bug on iOS or some permission is missing.

Solution:

Add permissions that was missing in Info.plist file:

<key>NSPhotoLibraryUsageDescription</key>

<string>Application needs to access photos</string>

for iOS 11+

<key>NSPhotoLibraryAddUsageDescription</key>

<string>Application needs to access photos</string>

Now Save Image action is working ok.