iphoneiosuiimagesavephotolibrary

iPhone: How do I get the file path of an image saved with UIImageWriteToSavedPhotosAlbum()?


I'm saving a merged image to the iPhone photo library using:

UIImageWriteToSavedPhotosAlbum(viewImage, self, @selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);

And getting the callback using:

- (void) savedPhotoImage:(UIImage*)image didFinishSavingWithError:(NSError *)error contextInfo: (void *)contextInfo { NSLog(@"%@", [error localizedDescription]);
NSLog(@"info: %@", contextInfo);}

What I would like to get is the path for where the image has been saved, so I can add it to an array which will be used to call up a list of saved items elsewhere in the app.

When I load the image using the picker it displays the path info. However when I save a created image, I can't find where to pull the saved image path.

I have search about the web, but most examples stop at the callback with a nice message to say the image was saved successfully. I would just like to be able to know where it was saved.

I understand one method might be to start defining my own paths, but as the method does that for me, I was just hoping it could tell me where it saved to.


Solution

  • I finally found out the answer. Apparently the UIImage methods strip out metadata and so using UIImageWriteToSavedPhotosAlbum is no good.

    However in ios4 Apple put in a new framework to handle the photo library called the ALAssetsLibrary.

    First you need to right click on the Targets and in the build part, add the AlAsset Framework to your project with the little + icon in the bottom left.

    Then add #import "AssetsLibrary/AssetsLibrary.h"; to the header file of your class.

    Finally you can use the following code:

    UIImage *viewImage = YOUR UIIMAGE  // --- mine was made from drawing context
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];  
    // Request to save the image to camera roll  
    [library writeImageToSavedPhotosAlbum:[viewImage CGImage] orientation:(ALAssetOrientation)[viewImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){  
        if (error) {  
            NSLog(@"error");  
        } else {  
                NSLog(@"url %@", assetURL);  
        }  
    }];  
    [library release];
    

    And that gets the path of the file you just saved.