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.
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.
Objective C implementation that works on iOS 10. ALAssetsLibrary seems to be deprecated so you should use PHAsset:
Simple Swift implementation:
Remember about:
import AssetsLibrary
As of Swift 3 and iOS8+ the .filename is not accessible any more. It is still available through self.valueForKey("filename"), but not quite legal though.
However, I found this answer in the question "iOS8 Photos Framework: How to get the name(or filename) of a PHAsset?" to be short, simple, and legal.
If you are building for iOS 9+ target, you will see a bunch of deprecation warnings with ALAssetsLibrary, i.e.:
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 thedidFinishPickingMediaWithInfo
UIImagePickerControllerDelegate method usefetchAssetsWithALAssetURLs
to get the filename:This will set
filename
to be something like, "IMG_0007.JPG".