uiimagepickercontroller - get the name of the image selected from photo library
I guess knowing the exact image name would not be an issue rather getting a unique name for the picked image would solve your purpose so that you can upload the image on server and track it via its name. May be this can help you
NSMutableString *imageName = [[[NSMutableString alloc] initWithCapacity:0] autorelease];
CFUUIDRef theUUID = CFUUIDCreate(kCFAllocatorDefault);
if (theUUID) {
[imageName appendString:NSMakeCollectable(CFUUIDCreateString(kCFAllocatorDefault, theUUID))];
CFRelease(theUUID);
}
[imageName appendString:@".png"];
After you pick the image from Picker you can generate a unique name and assign it to the Picked image.
Cheers
How can i get the name of image picked through photo library in iphone?
import AssetsLibrary in your file:
#import
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:
- In the project navigator, select your project
- Select your target
- Select the 'Build Phases' tab
- Open 'Link Binaries With Libraries' expander
- Click the '+' button
- Select your framework
- (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?
How do you get image file name from UIImagePickerController?
I found a way
if let asset = info["UIImagePickerControllerPHAsset"] as? PHAsset{
if let fileName = asset.value(forKey: "filename") as? String{
print(fileName)
}
}
This will return you the actual file name in the photo library.
Get Image name from photo Library
In the delegate method of UIImagePickerController called
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
you can use the info dictionary to get the reference url of the image that the user picked by using key UIImagePickerControllerReferenceURL
and get the file name from it.
Can i get the name of image from the photos in iPhone
Using the AssetsLibrary framework, you can get the referenceURL from the info dictionary and fetch the asset. Something like this:
NSURL *assetURL = [info objectForKey:UIImagePickerControllerReferenceURL];
__block NSString *fileName = nil;
ALAssetsLibrary *library = [[[ALAssetsLibrary alloc] init] autorelease];
[library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
fileName = asset.defaultRepresentation.fileName;
} failureBlock:nil];
How to get image name using UIImagePicker? I got image name but its working only in simulators not in actual devices
Please try this:
In your UIImagePickerControllerDelegate:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let asset = info[UIImagePickerControllerPHAsset] as? PHAsset {
let assetResources = PHAssetResource.assetResources(for: asset)
print(assetResources.first!.originalFilename)
}
dismiss(animated: true, completion: nil)
}
It may helps you. Thank you.
How to get file name in UIImagePickerController with Asset library?
I will suggest you to use Photos
Framework to get the name of the image, below is the code to get name of selected image
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let imageURL = info[UIImagePickerControllerReferenceURL] as? URL {
let result = PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil)
let asset = result.firstObject
print(asset?.value(forKey: "filename"))
}
dismiss(animated: true, completion: nil)
}
How can I get the image name when selecting an image
Short answer: You can't. The system doesn't give you the user's name for the file. I remember puzzling about this as well.
As I recall what it gives you is a pointer to an in-memory UIImage object, not even a URL to a system-named file on disk. (But it's been a while since I've used the image picker.)
Getting image name of iphone photo library
Have a look at ALAssetsLibrary which is available in iOS 4.0 and later.
Also, these links may help
How to get a photo's original filename in iOS?
iPhone: How do I get the file path of an image saved with UIImageWriteToSavedPhotosAlbum()?
Related Topics
Iphone App in Landscape Mode, 2008 Systems
Swift Native Functions to Have Numbers as Hex Strings
Navigation Controller Push View Controller
Watchkit Extension - No Matching Provisioning Profiles Found
Didregisterforremotenotificationswithdevicetoken Not Called in Ios8, But Didregister...Settings Is
How to Use Auto Layout to Move Other Views When a View Is Hidden
Send Post Request Using Nsurlsession
Fade/Dissolve When Changing Uiimageview'S Image
How to Allow Didset to Be Called During Initialization in Swift
How to Programmatically Connect to a Wifi Network Given the Ssid and Password
How to Implement Method Swizzling Swift 3.0
How to Deserialize a JSON String into an Nsdictionary? (For iOS 5+)
How to Make Drawrect Work Right Now