How to get file name in UIImagePickerController with Asset library?

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

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        var 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)
    }
    

提交回复
热议问题