一尘不染

如何使用Asset库在UIImagePickerController中获取文件名?

swift

我想从获取文件名UIImagePickerController。我不想使用ALAssetLibrary,因为它在iOS
9中已弃用。我使用了以下代码,但对于每个文件,它始终将图像名称返回为“ 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)
}

阅读 347

收藏
2020-07-07

共1个答案

一尘不染

我建议您使用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)
}
2020-07-07