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 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.
How can i get the name of image picked through photo library in iphone?
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:
- 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?
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
Related Topics
How to Stop Symbolicate Adding "<Redacted>" Pieces to iOS Crash Log
Swift: Navigate to New Viewcontroller Using Button
Typewriter Effect Text Animation
Swiftui Drag Gesture Across Multiple Subviews
Simulate Universal Gravitation for Two Sprite Kit Nodes
How to Change Status Bar Style - iOS 12
Disable Wkwebview for Opening Links to Redirect to Apps Installed on My Iphone
Push Notification -Didfinishlaunchingwithoptions
Maximum Number of Peripherals on Corebluetooth
Avfoundation Image Orientation Off by 90 Degrees in the Preview But Fine in Camera Roll
Toggling Airplane Mode in iOS Programmatically
Filetransfer Using Xmppframework in iOS
Create Movie from [Uiimage], Swift
Fatal Error: Nsarray Element Failed to Match the Swift Array Element Type