How to save and reload image using ALAsset?

sensslen picture sensslen · Feb 9, 2012 · Viewed 9.2k times · Source

I'm writing an app that lets users take Photos and assign them to a Device. For this I need to be able to store the URL of the taken Picture and reload the Picture afterwards.

I know there have been several discussions on that, but strangely none of the Ansers helped me...

So here's my Code for saving an Image:

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    ALAssetsLibrary * library = [[ALAssetsLibrary alloc] init];
    UIImage * image;

    // Request to save Image
    if ([picker sourceType] == UIImagePickerControllerSourceTypeCamera) {
        image = [info objectForKey:UIImagePickerControllerEditedImage];
        [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL * assertURL, NSError * error){
            if (error) {
                UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"PictureSaveErrorTitle", @"Title if saving picture fails") 
                                                                 message:NSLocalizedString(@"PictureSaveErrorMessage", @"Message if saving picture fails") 
                                                                delegate:nil 
                                                       cancelButtonTitle:NSLocalizedString(@"PictureSaveErrorOK", @"OK if saving picture fails") 
                                                       otherButtonTitles:nil];
                [alert show];
            } else {
                [self setUserImage:[assertURL absoluteString]];
                [imageOfDevice setImage:image];
            }
        }];
    } else {
        image = [info objectForKey:UIImagePickerControllerOriginalImage];
        [self setUserImage:[[info valueForKey:UIImagePickerControllerReferenceURL] path]];
    }

    [self dismissModalViewControllerAnimated:YES];
}

And this is for reloading the Image:

if (userImage != nil) {
        ALAssetsLibrary * library = [[ALAssetsLibrary alloc] init];
        [library assetForURL:[NSURL fileURLWithPath:userImage] 
                 resultBlock:^(ALAsset * asset){
                     [imageOfDevice setImage:[UIImage imageWithCGImage:[asset thumbnail]]];
                 }
                failureBlock:^(NSError * error){
                    NSLog(@"cannot get image - %@", [error localizedDescription]);
                }];
}

Does anybody see any fault?

Thanks for helping,

sensslen

Answer

Anthony McCormick picture Anthony McCormick · Feb 21, 2012

The simple solution is that you are passing in the absoluteString to instead of the NSUrl.

This code works for me inside picker did finish.

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
if( [picker sourceType] == UIImagePickerControllerSourceTypeCamera )
{
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
    [library writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error )
    {
        NSLog(@"IMAGE SAVED TO PHOTO ALBUM");
        [library assetForURL:assetURL resultBlock:^(ALAsset *asset )
        {
            NSLog(@"we have our ALAsset!");
        } 
                failureBlock:^(NSError *error )
         {
             NSLog(@"Error loading asset");
         }];
    }];
}