从iOS中的图像选择器获取视频网址

时间:2018-01-08 11:53:19

标签: ios swift uiimagepickercontroller

我在从照片库中选择视频网址时遇到问题。当图像选择器出现时,它无法选择,但我尝试选择它。它会自动压缩,图像选择器不会被忽略。

这是我的代码。

self.imagePickerController.sourceType = .savedPhotosAlbum
self.imagePickerController.delegate = self
self.imagePickerController.mediaTypes = [kUTTypeMovie as! String]

self.present(self.imagePickerController, animated: true, completion: nil)


func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
    var videoURL: NSURL? = nil
    videoURL = info["UIImagePickerControllerReferenceURL"] as? NSURL
    print(videoURL)

    imagePickerController.dismiss(animated: true, completion: nil)
}

5 个答案:

答案 0 :(得分:1)

Swift 4.2 中,您必须使用Apple提供的新枚举来捕获视频URL:

if let url = info[UIImagePickerController.InfoKey.mediaURL] as? URL {
    // Do something with the URL
}

您可以阅读有关mediaURL here的信息。

  

指定电影的文件系统URL。

答案 1 :(得分:0)

您可以从info获取视频网址,

videoURL = info[UIImagePickerControllerMediaURL] as? URL
print("videoURL:\(String(describing: videoURL))")
self.dismiss(animated: true, completion: nil)

答案 2 :(得分:0)

使用以下代码选择不压缩的视频:

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

let mediaType = info[UIImagePickerControllerMediaType] as? String
let videoString = kUTTypeVideo as? String
let movieString = kUTTypeMovie as? String

if (mediaType == videoString) || (mediaType == movieString) {
var videoRef = info[UIImagePickerControllerReferenceURL] as? URL
var refResult = PHAsset.fetchAssets(withALAssetURLs: [videoRef], options: nil) as? PHFetchResult
var videoRequestOptions = PHVideoRequestOptions()
videoRequestOptions.version() = .original
PHImageManager.default().requestAVAsset(forVideo: refResult.firstObject as? PHAsset ?? PHAsset(), options: videoRequestOptions, resultHandler: {(_ asset: AVAsset, _ audioMix: AVAudioMix, _ info: [AnyHashable: Any]) -> Void in
    if (asset is AVURLAsset) {
        var originURL: URL? = (asset as? AVURLAsset)?.url
        // Now you have the URL of the original video.
         picker.dismiss(animated: true, completion: nil)
    }
    else
   {
   picker.dismiss(animated: true, completion: nil)
   }
})
}
}

答案 3 :(得分:0)

就这么简单

extension HomeVC: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        let videoUrl = info[UIImagePickerControllerMediaURL] as! URL
        // Do something with the videoUrl

        DispatchQueue.main.async { // Dismiss it, remember using `picker`
            picker.dismiss(animated: true, completion: nil)
        }
    }
}

答案 4 :(得分:0)

在Swift 4.2中:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
    picker.dismiss(animated: true, completion: nil)
    guard let mediaURL = info[UIImagePickerController.InfoKey.mediaURL] as? URL else { return }
    // You can do whatever youo want here....
}