Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get UIImagePicker selected image name?

Does anyone know how to get the selected image name (the one from photo gallery) after presenting an UIImagePickerController?

like image 548
user1078065 Avatar asked Dec 06 '25 18:12

user1078065


1 Answers

With the help of ALAsset you can able to achieve this.

Step 1: Get the Asset for the selected Image from the UIImagePicker

Step 2: Get the ALAssetRepresentation for that Asset.

Step 3: ALAssetRepresentation class has a property called fileName

- (NSString *)filename

Returns a string representing the filename of the representation on disk.

This Sample Code will Helps you...

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

    NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        ALAssetRepresentation *representation = [myasset defaultRepresentation];
        NSString *fileName = [representation filename];
        NSLog(@"fileName : %@",fileName);
    };

    ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
    [assetslibrary assetForURL:imageURL 
                   resultBlock:resultblock
                  failureBlock:nil];

}

NOTE: It works only in iOS 5 and later.

like image 52
Aravindhan Avatar answered Dec 08 '25 10:12

Aravindhan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!