How can i get the name of image picked through photo library in iphone?

50,303

Solution 1

import AssetsLibrary in your file:

#import <AssetsLibrary/AssetsLibrary.h>

And, in - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

put

// get the ref url
NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];

// define the block to call when we get the asset based on the url (below)
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
{
    ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
    NSLog(@"[imageRep filename] : %@", [imageRep filename]);
};

// get the asset library and fetch the asset based on the ref url (pass in block above)
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil];

so you'll get the image name in log.

*don't forget to add existing framework: AssetsLibrary.framework Steps:

  1. In the project navigator, select your project
  2. Select your target
  3. Select the 'Build Phases' tab
  4. Open 'Link Binaries With Libraries' expander
  5. Click the '+' button
  6. Select your framework
  7. (optional) Drag and drop the added framework to the 'Frameworks' group

Source: http://www.raywenderlich.com/forums/viewtopic.php?f=2&p=34901 & How to "add existing frameworks" in Xcode 4?

Solution 2

If you are building for iOS 9+ target, you will see a bunch of deprecation warnings with ALAssetsLibrary, i.e.:

'assetForURL(_:resultBlock:failureBlock:)' was deprecated in iOS 9.0: Use fetchAssetsWithLocalIdentifiers:options: on PHAsset to fetch assets by local identifier (or to lookup PHAssets by a previously known ALAssetPropertyAssetURL use fetchAssetsWithALAssetURLs:options:) from the Photos framework instead

As the warning describes, you should use PHAsset. Using swift 2.x, for example, you will need to add import Photos to your file first. Then, in the didFinishPickingMediaWithInfo UIImagePickerControllerDelegate method use fetchAssetsWithALAssetURLs to get the filename:

if let imageURL = info[UIImagePickerControllerReferenceURL] as? NSURL {
    let result = PHAsset.fetchAssetsWithALAssetURLs([imageURL], options: nil)
    let filename = result.firstObject?.filename ?? ""
}

This will set filename to be something like, "IMG_0007.JPG".

Solution 3

Simple Swift implementation:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    if let referenceUrl = info[UIImagePickerControllerReferenceURL] as? NSURL {

        ALAssetsLibrary().assetForURL(referenceUrl, resultBlock: { asset in

            let fileName = asset.defaultRepresentation().filename()
            //do whatever with your file name

            }, failureBlock: nil)
        }
    }
}

Remember about: import AssetsLibrary

Solution 4

In Objective C, use the Photos framework and import Photos/Photos.h

Then, in your imagePickerController function add the following to get the filename of the image from the photo library

NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];
PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[refURL] options:nil];
NSString *filename = [[result firstObject] filename];

Solution 5

Though you may be able to retrieve the last path component and use it like a file name, it is not advisable to do so. These filenames are assigned by the system for iTunes to understand while syncing and are not meant for programmers to access as they could be replaced by some other images in future syncs.

A good round about for this is to assign the current Date as filenames, while saving to images picked from the gallery. You may save it in your documents or library directory and use a mapping PList file to map images to their filename.

Alternatively, you can also assign unique numbers as filenames and access the images using these values.

Share:
50,303
Sandeep Singh
Author by

Sandeep Singh

Updated on August 18, 2022

Comments

  • Sandeep Singh
    Sandeep Singh over 1 year

    I am picking an image from photo library in iphone application. How will i retrieve the actual image name.

    in .h class

    UIImageView * imageView;
    
    UIButton * choosePhotoBtn;
    

    in .m class

    -(IBAction) getPhoto:(id) sender 
    {
        UIImagePickerController * picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        if((UIButton *) sender == choosePhotoBtn)
        {
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        }
        else 
        { 
            picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        }
        [self presentModalViewController:picker animated:YES];
    }
    
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
    {
        [picker dismissModalViewControllerAnimated:YES];
        imageView.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    }
    

    How will i get the actual name of image ?

    I m new in iphone. Please help me.

    Thanks in advance.