Reputation: 93
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
}
}
Upvotes: 9
Views: 9085
Reputation: 208
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")
}
})
}
}
Upvotes: 0
Reputation: 3628
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)
{
Upvotes: 5