I want to get the file name from UIImagepickercontroller.I do not want to use ALAssetLibrary because it is deperecated in iOS 9.Please tell how to use it.I have used below code but it return me imagename as Asset.jpg always for each file.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let originalImage = (info[UIImagePickerControllerOriginalImage] as? UIImage)!
let url = info[UIImagePickerControllerReferenceURL] as! NSURL
imageData = UIImageJPEGRepresentation(originalImage, 100) as NSData?
let data = UploadData()
data.fileName = url.lastPathComponent
picker.dismiss(animated: true, completion: nil)
}
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)
}
Before iOS 11
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let imageURL = info[UIImagePickerControllerReferenceURL] as? URL {
let result = PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil)
let assetResources = PHAssetResource.assetResources(for: result.firstObject!)
print(assetResources.first!.originalFilename)
}
dismiss(animated: true, completion: nil)
}
After iOS 11
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)
}
Update 6th May 2018
Forced unwrapped optionals sometimes return nil, I think it's because user use Camera to capture a new image instead of selecting one from Photos library. I updated the code to return a generated name when I see nil.
Original answer
This works for me without having to rely on filename key (please excuse for force unwrap optionals :) )
extension MyViewController : UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
guard let image = info[UIImagePickerControllerEditedImage] as? UIImage else {
DDLogDebug("No image chosen")
return
}
if let url = info[UIImagePickerControllerReferenceURL] as? URL {
let assets = PHAsset.fetchAssets(withALAssetURLs: [url], options: nil)
if let firstAsset = assets.firstObject,
let firstResource = PHAssetResource.assetResources(for: firstAsset).first {
fileName = firstResource.originalFilename
} else {
fileName = generateNameForImage()
}
} else {
fileName = generateNameForImage()
}
DDLogDebug("File name = \(fileName)")
dismiss(animated: true)
}
func generateNameForImage() -> String {
return "IMG_random_string"
}
}
You can try this for iOS 11
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let asset = info[UIImagePickerControllerPHAsset] as? PHAsset {
if let fileName = (asset.value(forKey: "filename")) as? String {
//Do your stuff here
}
}
picker.dismiss(animated: true, completion: nil)
}
Make sure you add this NSPhotoLibraryUsageDescription to your Infor.plist
import Photos
func getFileName(info: [String : Any]) -> String {
if let imageURL = info[UIImagePickerControllerReferenceURL] as? URL {
let result = PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil)
let asset = result.firstObject
let fileName = asset?.value(forKey: "filename")
let fileUrl = URL(string: fileName as! String)
if let name = fileUrl?.deletingPathExtension().lastPathComponent {
print(name)
return name
}
}
return ""
}
call it inside the method "didFinishPickingMediaWithInfo" like so:
let fileName = getFileName(info: info)
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let selectedFileName = ""
if #available(iOS 11.0, *) {
if let imageUrl = info[.imageURL] as? URL {
selectedFileName = imageUrl.lastPathComponent
}
} else {
if let imageURL = info[.referenceURL] as? URL {
let result = PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil)
if let firstObject = result.firstObject {
let assetResources = PHAssetResource.assetResources(for: firstObject)
selectedFileName = assetResources.first?.originalFilename
}
}
}
picker.dismiss(animated: true, completion: nil)
}
Code:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let imageURL = info[UIImagePickerControllerReferenceURL] as NSURL
let imageName = imageURL.path!.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true).first as String
let localPath = documentDirectory.stringByAppendingPathComponent(imageName)
let image = info[UIImagePickerControllerOriginalImage] as UIImage
let data = UIImagePNGRepresentation(image)
data.writeToFile(localPath, atomically: true)
let imageData = NSData(contentsOfFile: localPath)!
let photoURL = NSURL(fileURLWithPath: localPath)
let imageWithData = UIImage(data: imageData)!
picker.dismissViewControllerAnimated(true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let asset = PHAsset.fetchAssets(withALAssetURLs: [info[UIImagePickerControllerReferenceURL] as! URL],options: nil).firstObject {
var resources = PHAssetResource.assetResources(for: asset)
let orgFilename: String = ((resources[0] as? PHAssetResource)?.originalFilename)!
print("filename:",orgFilename)
}
}
Here is how I did it in swift 4
if let url = info[UIImagePickerController.InfoKey.imageURL] as? URL {
fileName = url.lastPathComponent
fileType = url.pathExtension
}
For me, it is working perfect,
if let imgUrl = info[UIImagePickerControllerImageURL] as? URL{
let imgName = imgUrl.lastPathComponent
print(imgName)
let imgExtension = imgUrl.pathExtension
print(imgExtension)
}
来源:https://stackoverflow.com/questions/40627901/how-to-get-file-name-in-uiimagepickercontroller-with-asset-library