Question

I'm trying to save a UIImage to Photo Album. I've tried severl methods the last one is:

-(IBAction)captureLocalImage:(id)sender{

[photoCaptureButton setEnabled:NO];

// Save to assets library
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];

[library writeImageToSavedPhotosAlbum: imageView.image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error2)
 {
     //             report_memory(@"After writing to library");
     if (error2) {
         NSLog(@"ERROR: the image failed to be written");
     }
     else {
         NSLog(@"PHOTO SAVED - assetURL: %@", assetURL);
     }

     runOnMainQueueWithoutDeadlocking(^{
         //                 report_memory(@"Operation completed");
         [photoCaptureButton setEnabled:YES];
     });
 }];  

}

imageView is a UIImageView which contain the image I want to save. On log I got "PHOTO SAVED - assetURL: (null)" and the photo doesn't save to library.

What am I doing wrong?

Was it helpful?

Solution 4

I'm using GPUImage, Apparently there is some kind of problem with the library itself.

The code above is absolutely working. If you want to read more about this issue:

GPUImagePicture with imageFromCurrentlyProcessedOutput doesn't get UIImage

OTHER TIPS

just use this bellow line for save the image in your photo library

UIImageWriteToSavedPhotosAlbum(imageView.image,nil,nil,nil);

:)

Check out this sample.. which explains how to save photos using assets library..

you can even use..

UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo);

cehck out his link How to save picture to iPhone photo library?

For iOS 11+ needs Privacy - Photo Library Additions Usage Description in info.plist

Add NSPhotoLibraryAddUsageDescription in info.plist

Code:

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

Reference

Since iOS 8.1 UIKit framework has the UIImageWriteToSavedPhotosAlbum() function as vishy said.

I use it in this way:

 UIImageWriteToSavedPhotosAlbum(myImage, self, "image:didFinishSavingWithError:contextInfo:", nil)

and then

func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafePointer<Void>) {
    // check error, report image has been saved, ...
}

When you take a photo from camera, then you can save image in picker delegate:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info valueForKey:UIImagePickerControllerEditedImage];   
    [library writeImageToSavedPhotosAlbum:image.CGImage metadata:[info objectForKey:UIImagePickerControllerMediaMetadata] completionBlock:^(NSURL *assetURL, NSError *error) {
...
    }];



    [picker dismissViewControllerAnimated:YES completion:nil];
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top