Search code examples
iosswiftuiimagepickercontroller

info[UIImagePickerController.InfoKey.mediaURL] is sometimes nil


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.


Solution

  • 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 {
    
    
                    }
    
                }
    
        }
    
    }