info[UIImagePickerController.InfoKey.mediaURL] is sometimes nil

人盡茶涼 提交于 2021-02-08 05:09:37

问题


I am having a picker, and after successfully picking of video, I can't get a path to the asset. So I have this:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {



        if let videoURL = info[UIImagePickerController.InfoKey.mediaURL] as? URL {
            MediaManager.shared.sourceType = picker.sourceType
            self.metadata.videoURL = videoURL
        }
}

Generally in videos saved with the phone, or even many videos uploaded through iTunes or with different tools, I always get a path.

In this case, I transferred a hiRes video (720p) of 400mb, using AirDrop, and when I pick it, I get nil for its path...Am I missing something here?

I guess it is not because of the way it is transferred, cause I got a report for the same issue, using another app to transfer it to the phone.


回答1:


You need to handle it like this for all possible cases

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any] {

    picker.dismiss(animated: true)

    print("info[UIImagePickerController.InfoKey.mediaURL] referenceURL " , info[.referenceURL] )
    print("info[UIImagePickerController.InfoKey.mediaURL] phAsset " , info[.phAsset])
    print("info[UIImagePickerController.InfoKey.mediaURL] mediaURL " , info[.mediaURL])

    if let asset =  info[UIImagePickerController.InfoKey.phAsset] as? PHAsset {

        asset.getURL { (tempPath) in

            DispatchQueue.main.async {

            }
        }

    }
    else  if let media =  info[UIImagePickerController.InfoKey.mediaURL] as? URL {


    }
    else
        if let ref =  info[UIImagePickerController.InfoKey.referenceURL] as? URL {

            let res = PHAsset.fetchAssets(withALAssetURLs: [ref], options: nil)

            res.firstObject!.getURL { (tempPath) in

                DispatchQueue.main.async {


                }

            }

    }

}


来源:https://stackoverflow.com/questions/57807320/infouiimagepickercontroller-infokey-mediaurl-is-sometimes-nil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!