How to get file name in UIImagePickerController with Asset library?

后端 未结 13 1644
天命终不由人
天命终不由人 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:37

    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)
    }
    

提交回复
热议问题