Tec*_*ain 8 uiimagepickercontroller ios swift
我想从中获取文件名UIImagePickerController.我不想使用ALAssetLibrary,因为它在iOS 9中被取消了.请告诉我如何使用它.我使用了下面的代码,但它会像UIImagePickerController往常一样为每个文件返回imagename .
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)
}
Run Code Online (Sandbox Code Playgroud)
Raj*_*jat 18
我建议你使用PhotosFramework来获取图像的名称,下面是获取所选图像名称的代码
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)
}
Run Code Online (Sandbox Code Playgroud)
Gul*_*han 17
这是我如何在 swift 4 中做到的
if let url = info[UIImagePickerController.InfoKey.imageURL] as? URL {
fileName = url.lastPathComponent
fileType = url.pathExtension
}
Run Code Online (Sandbox Code Playgroud)
小智 9
在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)
}
Run Code Online (Sandbox Code Playgroud)
在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)
}
Run Code Online (Sandbox Code Playgroud)
2018年5月6日更新
强制解包的选项有时会返回nil,我认为这是因为用户使用相机来捕获新图像而不是从照片库中选择一个.当我看到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"
}
}
Run Code Online (Sandbox Code Playgroud)
斯威夫特 5、iOS 11+
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
let photo = info[.phAsset] as? PHAsset
let filename = photo?.value(forKey: "filename") as! String
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
17926 次 |
| 最近记录: |