1

I am using swift PHPhotoLibrary class to save image in photo library. Below is my code:

PHPhotoLibrary.sharedPhotoLibrary().performChanges( {
        let assetCreation = PHAssetCreationRequest.creationRequestForAssetFromImage(self.photoView.image!)
        let imageData = UIImageJPEGRepresentation((self.photoView?.image)!,1)
        let option = PHAssetResourceCreationOptions()
        assetCreation.addResourceWithType(PHAssetResourceType.FullSizePhoto, data: imageData!, options: option)


    },
    completionHandler: {
            success, error in
        if success {
            print("save image success" )
        } else {
            print(error)
        }
    })

But I always got this error message: Optional(Error Domain=NSCocoaErrorDomain Code=-1 "(null)") This error was printed by the else line: "print(error)"

Does anyone know what the problem is?

Joey Yi Zhao
  • 37,514
  • 71
  • 268
  • 523
  • which line gives you the error. – Sumeet Feb 04 '16 at 06:48
  • the line "print(error)" – Joey Yi Zhao Feb 04 '16 at 06:53
  • This Will help you to save UIImage to photo album [Save UIImage to Photo Album](http://stackoverflow.com/questions/178915/how-to-save-picture-to-iphone-photo-library) – Dhaval Patel Feb 04 '16 at 07:03
  • This link does solve my problem but I want to understand more. The code in the link is using UIImageWriteToSavedPhotosAlbum() method to save photo. I wander what the different between this one and PHPhotoLibrary. Why didn't my code not work? – Joey Yi Zhao Feb 04 '16 at 07:16
  • Just to confirm, the error you get is compile time right? – Sumeet Feb 04 '16 at 12:18
  • yes, it happens on compile time – Joey Yi Zhao Feb 15 '16 at 08:57
  • Are you using as Target IOS 10? If you do make sure add the following in your info.plst file You have to add the below permission in Info.plist. Permission in Info.plist Camera : Key : Privacy - Camera Usage Description Value : $(PRODUCT_NAME) camera use Photo : Key : Privacy - Photo Library Usage Description Value : $(PRODUCT_NAME) photo use Copied from : http://stackoverflow.com/questions/39631256/request-permission-for-camera-and-library-in-ios-10-info-plist – JuValencia Feb 19 '17 at 10:18

0 Answers0