12

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?

Idan
  • 9,880
  • 10
  • 47
  • 76

6 Answers6

14

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

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

:)

Paras Joshi
  • 20,427
  • 11
  • 57
  • 70
  • 2
    I think that the OP is specifically asking how to save to a particular album, not just generically how to save the image. (I do realize I'm late to the game here, but it seems to me to be an important distinction) – Max von Hippel Jul 17 '16 at 20:40
  • Iran said he was having trouble saving an image to the photo album. This makes it sound like he wants to save to a specific album. 1. Idan did not accept your answer. He accepted his own answer. Scroll down and look. 2. You are not wrong, but you are not answering the whole question. In my opinion, @vishy below does a much better job of specifically answering the explicit question, of how to save not to camera roll but to a specific album. – Max von Hippel Jul 18 '16 at 06:03
  • Paras, we obviously disagree, and any further communication should be over private chat. Basically, from my perspective, @vishy had already answered the question when you wrote your answer. Your answer has less information than vishy's, and you wrote it after vishy, so it seems both unnecessary and inferior to me. It is not a big deal regardless. My one down vote will not effect your cred in any significant manner. – Max von Hippel Jul 18 '16 at 06:09
  • 1
    @MaxvonHippel No issue, I just gave my opinion and you're also right from your side. Thanks :) – Paras Joshi Jul 18 '16 at 06:12
4

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

Lal Krishna
  • 15,485
  • 6
  • 64
  • 84
2

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?

Community
  • 1
  • 1
vishy
  • 3,241
  • 1
  • 20
  • 25
  • I've tried those and result the same. Is it possible it's because I'm taking photo from local library before and don't close it well or something like that? I can see the photo I'm getting but can't save it. I'm desperate... – Idan Oct 04 '12 at 19:27
  • 1
    I've already solved that, The problem wasn't with this code snippet. I've posted the exact problem above. Thanks. – Idan Oct 05 '12 at 10:46
1

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

Community
  • 1
  • 1
Idan
  • 9,880
  • 10
  • 47
  • 76
0

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, ...
}
abanet
  • 1,327
  • 17
  • 22
0

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];
}
Jay Bhalani
  • 4,142
  • 8
  • 37
  • 50