Question

I have this code who write / Create a new image in iOS photo library:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    // Request to save the image to camera roll
    [library writeImageToSavedPhotosAlbum:[imagem CGImage] orientation:(ALAssetOrientation)[imagem imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){

//code here...

}

The code that does this, is probably stamped in code writeImageToSavedPhotosAlbum, but in this time I don`t need to create a new image in library, I need to get the Path of the image who exist in the library, In my case the image is inside the variable 'imagem'.

What code I could change the writeImageToSavedPhotosAlbum to get the Path of image?

[EDIT]

I get the image inside the variable imagem in this format:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

    UIImage *imagem  = [info objectForKey:UIImagePickerControllerOriginalImage];
    [imagePicker dismissViewControllerAnimated:YES completion:nil];
}
Was it helpful?

Solution

You have:

-(void)imagePickerController:(UIImagePickerController *)picker 
        didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *imagem  = [info objectForKey: UIImagePickerControllerOriginalImage];
}

But you say you don't need the image; you just need the path. That would be:

-(void)imagePickerController:(UIImagePickerController *)picker 
        didFinishPickingMediaWithInfo:(NSDictionary *)info{
    NSURL *imageurl  = [info objectForKey: UIImagePickerControllerReferenceURL];
}

Now you have the URL and you can store it; you can then retrieve the actual image later if you need it.

To get from an asset URL to an image, see my answer here: https://stackoverflow.com/a/22968941/341994

OTHER TIPS

Use ALAssetRepresentation...

[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {

    // Within the group enumeration block, filter to enumerate just photos.
    [group setAssetsFilter:[ALAssetsFilter allPhotos]];

    // Chooses the photo at the last index
    [group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:[group numberOfAssets] - 1] options:0 usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {

        // The end of the enumeration is signaled by asset == nil.
        if (alAsset) {
            ALAssetRepresentation *representation = [alAsset defaultRepresentation];
            UIImage *latestPhoto = [UIImage imageWithCGImage:[representation fullScreenImage]];

Or if you want the path...

            NSURL *imageUrl = [representation url];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top