John
John

Reputation: 1888

Getting video using UIImagePickerController

I'm trying to get a selected video from an iOS app using UIImagePickerController, and so far when the button is clicked it opens the PhotoLibrary perfectly fine and I select a video and it says compressing video, so it's acting like its selecting it but after that nothing really happens. And I can't really seem to figure out why.

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

Am I missing something? It looks to me like it should be working

Upvotes: 1

Views: 1173

Answers (1)

Teja Nandamuri
Teja Nandamuri

Reputation: 11201

The code you have will only check for the image types. If you want to get the selected video information, you need to look for the key UIImagePickerControllerMediaType and you need to use the delegate method didFinishPickingMediaWithInfo not the didFinishPickingImage

  NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];

Upvotes: 1

Related Questions