21

I'm trying to save an image to the photo library in Swift 3 (I'm working with Xcode 8).

ViewController Code:

func shareImage(image: UIImage) {
    let items = [image]

    var activityVC: UIActivityViewController = UIActivityViewController(activityItems: items, applicationActivities: nil)
    let excludeActivities: [UIActivityType] = [UIActivityType.airDrop,
                                               UIActivityType.assignToContact,
                                               UIActivityType.addToReadingList,
                                               UIActivityType.copyToPasteboard]

    activityVC.excludedActivityTypes = excludeActivities

    self.present(activityVC, animated: true, completion: nil)
}

When I run the application, and click on the button to take the screenshot (converting it to image, ..., that's all working perfectly), the app asks for permission to access the photo library, I tap the "OK" button, and then the app crashes. The image is not saved in the photo library.

The only clue I get from Xcode is the following:

2016-09-28 11:24:27.216043 Ajax Kids[4143:1545362] [error] error: -addPersistentStoreWithType:SQLite configuration:(null) URL:file:///var/mobile/Media/PhotoData/Photos.sqlite?readonly_shm=1 options:{
NSPersistentStoreFileProtectionKey = NSFileProtectionCompleteUntilFirstUserAuthentication;
NSReadOnlyPersistentStoreOption = 1;
NSSQLitePersistWALOption = 1;
NSSQLitePragmasOption =     {
    "journal_mode" = WAL;
};
} ... returned error Error Domain=NSCocoaErrorDomain Code=256 "The file couldn’t be opened." UserInfo={reason=Failed to access file: 1} with userInfo dictionary {
reason = "Failed to access file: 1";
}
2016-09-28 11:24:27.216433 Ajax Kids[4143:1545362] [Migration] Unexpected error opening persistent store <private>, cannot attempt migration <private>)
2016-09-28 11:24:27.216568 Ajax Kids[4143:1545362] [Migration] Failed to open store <private>.  Requires update via assetsd (256: <private>)

Does anyone have any idea how to fix this?

Thanks in advance!

UPDATE

Sharing the image on Social Media works fine, so the problem is specified to saving the image in the photo library.

Efren
  • 4,003
  • 4
  • 33
  • 75
Charlotte1993
  • 589
  • 4
  • 27
  • Share code when you try to save image – Oleg Gordiichuk Sep 28 '16 at 09:41
  • @OlegGordiichuk That is all the code I have written to share/save image. The OS takes care of the saving code or posting the image on Facebook for example. That is code that you don't need to write with an UIAcrtivityViewController. – Charlotte1993 Sep 28 '16 at 09:44
  • You should add permission in the plait file – Oleg Gordiichuk Sep 28 '16 at 09:48
  • @OlegGordiichuk Been there, done that: Privacy - Photo Library Usage Description is added and filled in in the info.plist – Charlotte1993 Sep 28 '16 at 09:49
  • @OlegGordiichuk Yes, (btw: Privacy - Photo Library Usage Description (in Property List of info.plist) is exact the same thing as NSPhotoLibraryUsageDescription (in Source code of info.plist)) – Charlotte1993 Sep 28 '16 at 10:02
  • From the error it is possible to conclude that is it defiantly problem with permission – Oleg Gordiichuk Sep 28 '16 at 10:05
  • I am getting the same error. I have the `NSPhotoLibraryUsageDescription` key in my plist. The crash occurs when I tap the option to open the Photo Library to choose a photo to upload through a web view. – SeanR Oct 25 '16 at 06:23
  • From SeanR comment and answer below it might be related to other libraries. What dependencies are you using? – Efren Aug 01 '17 at 00:40

4 Answers4

53

Add new records in your new InfoPlist.strings file.

<key>NSPhotoLibraryAddUsageDescription</key>
<string>$(PRODUCT_NAME)</string>

UPD: iOS 11 key

Andrey Oshev
  • 846
  • 7
  • 17
16

On iOS 11, there is a new property called NSPhotoLibraryAddUsageDescription, similar to NSPhotoLibraryUsageDescription. See https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html

Tiois
  • 1,519
  • 1
  • 15
  • 32
  • @Tiois I am not able to save photo in photo library. I am not getting any error. Its displaying "Image Saved". But in photo library photo is not there. – Ekta Padaliya Jan 29 '18 at 08:04
  • @EktaPadaliya probably that you are using an empty UIImage, make sure you are using the right UIImage (containing your image). – Tiois Jan 29 '18 at 15:03
1

Try to force request permissions like this:

PHPhotoLibrary.requestAuthorization { status in
  if status == .authorized {
    //do things
  }
}

do not forget import Photos. Hope this helps.

Andrey M.
  • 3,021
  • 29
  • 42
0

I found the culprit in my particular case. We are using Leanplum for analytics and push notifications. The Leanplum.syncResourcesAsync method was causing the opening of the photo library to crash. It took a couple of days to find as I wasn't aware Leanplum was doing anything to hook into a user's photo library... which in itself is concerning.

We weren't using the functionality this particular method brings, so were able to just remove the method call and the photo library stopped crashing.

SeanR
  • 7,899
  • 6
  • 27
  • 38