无法检索 URL 相机胶卷中的视频 Swift 5

Unable to retrieve URL for video in camera roll Swift 5

下面是我的代码:

@IBAction func selectVideoButton(_ sender: UIButton) {
    let videoPicker = UIImagePickerController()
    videoPicker.sourceType = UIImagePickerController.SourceType.photoLibrary
    videoPicker.mediaTypes = [kUTTypeImage as String, kUTTypeMovie as String]
    videoPicker.delegate = self
    present(videoPicker, animated: true, completion: nil)
}


func retrieveURL(_picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any]) {
    if let videoURL = info[.mediaURL] {
        print("Here is the URL: \(videoURL)")
        return
    }
}

我没有收到任何错误消息,选择视频后应用程序也没有崩溃,只是似乎什么也没做。抱歉,如果某处有此问题的答案,找不到与 swift 5 或有效的答案相关的问题。

我尝试将 "Any" 更改为字符串,然后相应地修改视频 URL,但这也没有用。

您需要正确的委托方法

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

    if let videoURL = info[.mediaURL] {
        print("Here is the URL: \(videoURL)") 
    }
    picker.dismiss(animated:true,completion:nil) // add this 
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
   let videoURL = info[UIImagePickerControllerMediaURL] as? URL
   print(videoURL)
   imagePickerController.dismissViewControllerAnimated(true, completion: nil)
}