使用 UIImagePickerController 获取视频

Getting video using UIImagePickerController

我正在尝试使用 UIImagePickerController 从 iOS 应用程序中获取 selected 视频,到目前为止,当单击该按钮时,它可以很好地打开 PhotoLibrary,我 select 一个视频,它说 compressing video,所以它的表现就像它 select 一样,但之后什么都没有发生。我似乎真的想不通为什么。

var picker = UIImagePickerController()
var imag = UIImagePickerController()

...

@IBAction func selectMediaAction(sender: UIButton) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary){
        picker.delegate = self
        picker.allowsEditing = false
        picker.mediaTypes = [kUTTypeMovie as String]
        self.presentViewController(picker, animated: true, completion: nil)
    }
}

func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
    let media = editingInfo[UIImagePickerControllerOriginalImage]
    let imageURL = media?.filePathURL as NSURL?
    mediaPath = imageURL
    mediaName.text = imageURL!.lastPathComponent
    self.dismissViewControllerAnimated(true, completion: nil)
    toggleSubmit()
}

@IBAction func createMediaAction(sender: UIButton) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera){
        imag.delegate = self
        imag.allowsEditing = false
        imag.sourceType = UIImagePickerControllerSourceType.Camera
        imag.mediaTypes = [kUTTypeMovie as String]
        self.presentViewController(imag, animated: true, completion: nil)
    } else{
        let alert = UIAlertController(title: nil, message: "There is no camera available on this device", preferredStyle: UIAlertControllerStyle.Alert)
        alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: nil))
        self.presentViewController(alert, animated: true, completion: nil)
    }
}

我错过了什么吗?在我看来它应该可以工作

您的代码只会检查图像类型。如果要获取选中的视频信息,需要寻找key UIImagePickerControllerMediaType 并且需要使用委托方法didFinishPickingMediaWithInfo 而不是didFinishPickingImage

  NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];