How to get file name in UIImagePickerController with Asset library?

后端 未结 13 1607
天命终不由人
天命终不由人 2020-12-03 14:55

I want to get the file name from UIImagePickerController. I do not want to use ALAssetLibrary because it is deprecated in iOS 9. I have used the following code

13条回答
  •  独厮守ぢ
    2020-12-03 15:56

    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)
    

提交回复
热议问题