IOS

IOS Get image name in Image Picker ViewConroller

xcode closeup
In brief
To get the image name from an Image Picker ViewController in iOS, access the reference URL from the editingInfo dictionary and extract the last path component.

Get the name of the chosen image from library using the following code in using Image Picker ViewController

- (void)imagePickerController:(UIImagePickerController *)picker

        didFinishPickingImage:(UIImage *)img

                  editingInfo:(NSDictionary *)editingInfo{

    [picker dismissModalViewControllerAnimated:YES];
    NSURL *imagePath = [editingInfo objectForKey:@"UIImagePickerControllerReferenceURL"];
    NSString *imageName = [imagePath lastPathComponent];
    NSLog(@"image name %@",imageName);

}

Here imageName variable will contains the name of the file chosen.

2 Comments

  1. Jyoti

    Thanks Vipin,
    This solution helps me in swift. I have used the same concept in swift to get the filename.
    In Swift it works like:-
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {

    let gotImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    let imagePath: NSURL = (info[UIImagePickerControllerReferenceURL] as! NSURL)
    let imageName: String = imagePath.lastPathComponent!
    print(imageName)
    self.dismissViewControllerAnimated(true, completion: nil)
    }

  2. preeti

    if image is captured from camera so how can I get any name.

Leave a Comment