使用UIImagePickerController

时间:2016-01-25 21:14:49

标签: ios swift uiimagepickercontroller

我试图使用UIImagePickerController从iOS应用中获取所选视频,到目前为止,单击该按钮时,它会完全打开PhotoLibrary并选择一个视频并显示compressing video,所以它的行为就像选择它一样,但之后并没有真正发生。而我似乎无法弄明白为什么。

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)
    }
}

我错过了什么吗?它看起来像我应该工作

1 个答案:

答案 0 :(得分:1)

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

  NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];