13

My app crashes every time when I try to save image using photo framework.

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

      _mChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[info valueForKey:UIImagePickerControllerOriginalImage]];

      [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

      _mChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[info valueForKey:UIImagePickerControllerOriginalImage]];

      } completionHandler:^(BOOL success, NSError *error) {

          if (success) {

              PHObjectPlaceholder *assetPlaceholder = _mChangeRequest.placeholderForCreatedAsset;
          }
          else {

             NSLog(@"write error : %@",error);
          }
    }];
}

crash : NSInternalInconsistencyException', reason: 'This method can only be called from inside of -[PHPhotoLibrary performChanges:completionHandler:] or -[PHPhotoLibrary performChangesAndWait:error:]'
jones
  • 749
  • 7
  • 34
Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70

4 Answers4

43

All you need to do is trigger a creation request. As the error says, you can access the change request only inside the performChanges block.

So to save the image you would do something like this:

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    [PHAssetChangeRequest creationRequestForAssetFromImage:[info valueForKey:UIImagePickerControllerOriginalImage]];
} completionHandler:^(BOOL success, NSError *error) {
    if (success) {
         NSLog(@"Success");
    }
    else {
        NSLog(@"write error : %@",error);
    }
}];

In case you need to do something with the placeholder of the newly created asset, you can access it inside the same performChanges block:

PHAssetChangeRequest *changeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:[info valueForKey:UIImagePickerControllerOriginalImage]];
PHObjectPlaceholder *assetPlaceholder = changeRequest.placeholderForCreatedAsset;
Artal
  • 8,933
  • 2
  • 27
  • 30
4

In Swift 3 I do this to save the video to the library.

if mediaType.isEqual(to: (kUTTypeMovie as NSString) as String) {
    if let videoURL = info[UIImagePickerControllerMediaURL] as? URL {
        PHPhotoLibrary.shared().performChanges({
            _ = PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: videoURL)
        }, completionHandler: { (success, error) in
            if success {
                print("ok")
                let videoData = NSData(contentsOf: videoURL)
                // use videoData here if needed...
                if let posterImage = self.firstFrame(videoURL: videoURL) {
                    self.imageView.image = posterImage
                }
                picker.dismiss(animated: true) { () -> Void in
                }
            } else {
                print(error?.localizedDescription)
            }
        })
    }
}
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
Niklas
  • 1,322
  • 14
  • 11
  • Wow thanks! This syntax is so strange to me. Why the need for the `in` keyword? Also the parameters defined inside the function block. – MichaelGofron Mar 02 '19 at 16:59
3

Here is an example of code how you can write/save image to Photo Library using UIImageWriteToSavedPhotosAlbum function:

- (void)saveImage:(UIImage *)image {
    UIImageWriteToSavedPhotosAlbum(self.image, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
{
    if (!error) {
        // saved successfully

        PHFetchOptions *fetchOptions = [PHFetchOptions new];
        fetchOptions.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
        PHAsset *asset = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeImage options:fetchOptions].firstObject;
        if (asset != nil) {
            // here you can use asset of your image
        }
    } else {
        NSLog(@"save image error: %@", error);
    }
}

Don't forget to add into your Info.plist a key-value pair Privacy - Camera Usage Description with description of usage.

Alena
  • 1,080
  • 12
  • 20
2
  1. delete third line of the code
  2. Verify that the _mChangeRequest is __block variable
  3. compile and run

you will see that image in the photos app

you will change code probably like this...

- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
    __block PHAssetChangeRequest *_mChangeRequest = nil;
    [[PHPhotoLibrary sharedPhotoLibrary]
            performChanges:^{
                UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
                _mChangeRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
            }, completionHandler :^(BOOL success, NSError *error) {
        if (success) {
            PHObjectPlaceholder *assetPlaceholder = _mChangeRequest.placeholderForCreatedAsset;
        } else {
            NSLog(@"write error : %@", error);
        }
    }];
}
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
food bowl
  • 39
  • 2