简体   繁体   中英

How to open movie gallery in swift

i'm trying to open the movie's gallery on ipad, but I have this error and i don't know why.

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Source type must be UIImagePickerControllerSourceTypeCamera'

This is my code:

func GaleriaVideo() {
   if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum) {
        println("Galeria Video")

        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
        imagePicker.mediaTypes = [kUTTypeMovie]
        imagePicker.allowsEditing = false

        imagePicker.showsCameraControls = false

        imagePicker.modalPresentationStyle = .Custom
        self.presentViewController(self.imagePicker, animated: true, completion: nil)
    }
}

Similar code works for me to photo gallery, but i don't know why this not works.

Can you tell me what's wrong in my code, or what i have to search to do this? i'm trying this for few days ago and i'm feel lost.

Thanks you,

EDIT:

This is my Photo gallery and it works:

    func GaleriaImagen() {

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
        println("Galeria Imagen")

        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
        imagePicker.allowsEditing = false

        imagePicker.modalPresentationStyle = .Custom
            self.presentViewController(self.imagePicker, animated: true, completion: nil)

    }
}
func imagePickerControllerDidCancel(picker: UIImagePickerController!) {

    self.dismissViewControllerAnimated(false, completion: nil)
}

func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
    self.dismissViewControllerAnimated(true, completion: nil)
}

Try this

func GaleriaVideo() {
   if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum) {
        println("Galeria Video")

        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
        imagePicker.mediaTypes = [kUTTypeMovie]
        imagePicker.allowsEditing = false

        imagePicker.showsCameraControls = false

        imagePicker.modalPresentationStyle = .Custom
        self.presentViewController(self.imagePicker, animated: true, completion: nil)
    }
}

Add: UINavigationControllerDelegate, UIImagePickerControllerDelegate

func gallery() {
  if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.PhotoLibrary) {
    var img = UIImagePickerController()
    img.delegate = self
    img.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
    //img.mediaTypes = [kUTTypeImage]; //whatever u want type
    img.allowsEditing = false
    self.presentViewController(img, animated: true, completion: nil)
  }
}

func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
  self.dismissViewControllerAnimated(true, completion: nil)    
}

Expanding on Aju answer this will work for swift 2

func GaleriaVideo() {
  if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){
    print("Galeria Video")
    imagePicker.delegate = self
    imagePicker.sourceType = UIImagePickerControllerSourceType.SavedPhotosAlbum
    imagePicker.mediaTypes = [kUTTypeMovie as String]
    imagePicker.allowsEditing = false
    self.presentViewController(imagePicker, animated: true, completion: nil)
  }
}

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
  self.dismissViewControllerAnimated(true, completion: nil)
  //Here you can manipulate the adquired video
}

Swift3/4 :

func GaleriaImagen() {        
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.savedPhotosAlbum){
            print("Galeria Imagen")
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum
            imagePicker.allowsEditing = false
            imagePicker.modalPresentationStyle = .custom
            self.present(self.imagePicker, animated: true, completion: nil)
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM