12

for an app I'm developing, I use UIImagePickerController to shoot a picture and store it in camera roll:

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info {
//... some stuff ...

        UIImageWriteToSavedPhotosAlbum([info objectForKey:@"UIImagePickerControllerOriginalImage"], nil, nil, nil);

}

the image is saved, now I need to get its reference url so I try to enumerate camera roll and get the last image, but I always get the image before the one I just shot.

Anybody has an idea how to get the reference of the just saved picture?

Thanks, Max

masgar
  • 1,875
  • 2
  • 20
  • 32

1 Answers1

12

This solution should fix the problem:

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeImageToSavedPhotosAlbum:((UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage]).CGImage
                                 metadata:[info objectForKey:UIImagePickerControllerMediaMetadata]
                          completionBlock:^(NSURL *assetURL, NSError *error) {
                              NSLog(@"assetURL %@", assetURL);
                          }];

et voila:

assetURL assets-library://asset/asset.JPG?id=1611E84C-24E2-4177-B49A-1C57B4A9C665&ext=JPG
masgar
  • 1,875
  • 2
  • 20
  • 32
  • Can you suggest me something more on how you save the image in camera roll? I can save the image but the assets URL returns "null" – Sabarish Nov 27 '13 at 11:45
  • You have to change completion block. I the example above the completion block simply logs the assetURL, it should be something like: ALAssetsLibrary *al = [addPhoto defaultAssetsLibrary]; [al assetForURL:assetURL resultBlock:resultblock failureBlock:failureblock]; where defaultAssetsLibrary is: + (ALAssetsLibrary *)defaultAssetsLibrary { static dispatch_once_t pred = 0; static ALAssetsLibrary *library = nil; dispatch_once(&pred, ^{ library = [[ALAssetsLibrary alloc] init]; }); return library; } – masgar Dec 02 '13 at 18:03