9

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
    }
}
1
  • 1
    imagePicker.mediaTypes Should be set like this --> imagePicker.mediaTypes = [kUTTypeImage as String , kUTTypeImage as String]
    – Husam
    Jan 19, 2016 at 18:02

2 Answers 2

5

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)
          {
6
  • Thanks for the input but that did not work. It caused a trap error.
    – Matthew
    Mar 25, 2015 at 14:46
  • I hope you did not copy / paste ** } ** it is only to show where you may insert }
    – ares777
    Mar 25, 2015 at 14:51
  • No, i simply closed the if bracket of kUTTypeImage.
    – Matthew
    Mar 25, 2015 at 14:58
  • 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)
    – Matthew
    Mar 25, 2015 at 14:59
  • Thanks, this along with let url = info[UIImagePickerControllerMediaURL] as NSURL let videoPath = url.relativePath ; fixed the issue
    – Matthew
    Mar 26, 2015 at 16:00
0

You can save video using this, hope it helps -

public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
    let mediaType = info[UIImagePickerControllerMediaType] as! NSString

    if mediaType.isEqual(to: kUTTypeMovie as NSString as String)
    {
        // Is Video
        let url: URL = info[UIImagePickerControllerMediaURL] as! URL

        assetsLibrary.writeVideoAtPath(toSavedPhotosAlbum: url, completionBlock: {
            (nsurl, error) -> Void in
            if let theError = error{
                print("Error saving video = \(theError)")
                self.dismiss(animated: true, completion: nil)
            }
            else {
                print("no errors happened")
            }
        })
    }
}
2
  • what is assetsLibrary here? how to reach it?
    – imike
    Jan 7, 2019 at 14:57
  • You will find details about ALAssetsLibrary in AppleDocumentation. Here "assetLibrary" is an instance of ALAssetsLibrary that provides access to the videos and photos that are under the control of the Photos application. Link is here - developer.apple.com/documentation/assetslibrary/alassetslibrary
    – Shawon91
    Jan 17, 2019 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.