我想从UIImagepickercontroller
获取文件名。我不想使用ALAssetLibrary,因为它在iOS 9中被取消了。请告诉我如何使用它。我使用了下面的代码,但它总是为每个文件返回imagename为Asset.jpg
。
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)
}
我建议你使用Photos
Framework来获取图像的名称,下面是获取所选图像名称的代码
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)
}
对我来说,它是完美的,
if let imgUrl = info[UIImagePickerControllerImageURL] as? URL{
let imgName = imgUrl.lastPathComponent
print(imgName)
let imgExtension = imgUrl.pathExtension
print(imgExtension)
}
在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)
}
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)
}
2018年5月6日更新
强制解包的选项有时返回nil
,我认为这是因为用户使用Camera捕获新图像而不是从Photos库中选择一个。当我看到nil时,我更新了代码以返回生成的名称。
原始答案
这对我有用,而不必依赖filename
键(请原谅强行解包选项:))
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"
}
}
你可以试试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)
}
确保将此NSPhotoLibraryUsageDescription添加到Info.plist中
导入照片
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 ""
}
在方法“didFinishPickingMediaWithInfo”中调用它,如下所示:
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)
}
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)
}
}
这是我在swift 4中的表现
if let url = info[UIImagePickerController.InfoKey.imageURL] as? URL {
fileName = url.lastPathComponent
fileType = url.pathExtension
}