Search code examples
iosswiftuiimagepickercontroller

saving video; using UIImagePickerController; IOS 8 Swift


I'm new to app developing so this may be a trivial question but please HELP!!

I am trying to save a video to the Camera Roll using the UIImagePickerController class. So far I have been successful pulling up the camera and saving an image. I have also been able to record video, but when I press "use video" it does not save to the camera roll.

Below is the attached didFinishPickingMediaWithInfo function.

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject])
{
    let mediaType = info[UIImagePickerControllerMediaType] as NSString

    self.dismissViewControllerAnimated(true, completion: nil)

    if mediaType.isEqualToString(kUTTypeImage as NSString)
    {
        let image = info[UIImagePickerControllerOriginalImage] as UIImage

        if (newMedia == true)
        {
            UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil)
        }

        else if mediaType.isEqualToString(kUTTypeMovie as NSString)
        {
            let videoPath = info[UIImagePickerControllerMediaURL] as NSString

            if(newMedia == true)
            {
                UISaveVideoAtPathToSavedPhotosAlbum(videoPath, self,
                    "image:didFinishSavingWithError:contextInfo:", nil)
            }
        }
    }
}

Here is the useVideo routine.

@IBAction func useVideo(sender: AnyObject)
{
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)
    {

        let imagePicker = UIImagePickerController()

        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.Camera
        imagePicker.mediaTypes = [kUTTypeMovie as NSString]
        imagePicker.allowsEditing = false

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

        newMedia = true

    }
}

Here is the cameraRoll.

@IBAction func useCameraRoll(sender: AnyObject) {
    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum){

        let imagePicker = UIImagePickerController()

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

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

        newMedia = false
    }
}

Solution

  • You have mismatched if close.

         if mediaType.isEqualToString(kUTTypeImage as NSString)
             {
                 let image = info[UIImagePickerControllerOriginalImage] as UIImage
    
                 if (newMedia == true)
                 {
                     UIImageWriteToSavedPhotosAlbum(image, self, "image:didFinishSavingWithError:contextInfo:", nil)
                 }
    

    } // <--here you close if condition for image add }

         else if mediaType.isEqualToString(kUTTypeMovie as NSString)
              {