0

I'm trying to save a video recorded with camera using the UIImagePickerController. I've tried two different ways, and both are not working at all.

First attempt:

if let pathURL = info["UIImagePickerControllerMediaURL"] as! URL?, mediaType == kUTTypeMovie {
    PHPhotoLibrary.shared().performChanges({
        PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: pathURL)
    }, completionHandler: { (isSuccessfull, error) in
        if error != nil {
            // ERROR
            // error.localizedDescription value below
            // "The operation couldn’t be completed. (Cocoa error -1.)"
        } else if isSuccessfull {
            // SUCCESS
        }
    })
}

On that attempt, I'm always getting the error "The operation couldn’t be completed. (Cocoa error -1.)"

Second attempt:

if let filePath = info["UIImagePickerControllerMediaURL"] as! NSURL,
   let stringPath = filePath.path {
    UISaveVideoAtPathToSavedPhotosAlbum(stringPath, self, #selector(Controller.videoSaved(videoPath:didFinishSavingWithError:contextInfo:)), nil)
}

This is not working either... The callback, the error is nil, but the video is not saved into the camera roll. Also, when I use the UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(stringPath), this method also returning me false all the time. I'm not able to understand why the video recorded would be incompatible to save.

I really don't know what I'm doing wrong. What am I missing? A configuration in plist?

Here's how look like the value of info["UIImagePickerControllerMediaURL"]: file:///private/var/mobile/Containers/Data/Application/2B9BE04A-17B3-49CE-B4BA-C45F183E9A64/tmp/837294779101__C4825AFE-8140-420F-ACD0-64623C7A4753.MOV

For pictures taken with camera, PHPhotoLibrary method works very fine... It's only videos that is not able to be saved.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
manonthemoon
  • 2,611
  • 8
  • 26
  • 40
  • check out this answer in question https://stackoverflow.com/questions/9991332/how-to-save-recorded-video-into-photo-album – Jitendra Modi Nov 25 '17 at 09:09
  • Thank you Jitendra. I tried to put that same code, directly in the delegate method and it worked. I was originally making this save operation after an API call. My assumption is that the file at this path probably isn't available after few seconds maybe... – manonthemoon Nov 25 '17 at 09:40
  • You can get the asset from the info with this key `UIImagePickerControllerPHAsset`. – Infinity James Nov 25 '17 at 16:15
  • @InfinityJames, when I record a video, the dictionary contains only those keys : `UIImagePickerControllerMediaURL`, and `UIImagePickerControllerMediaType`. – manonthemoon Nov 26 '17 at 17:15

1 Answers1

0

Not sure in 100% but I believe that it wasn't working for me because I was saving the video only after a request to my API was successful. My assumption is that the path URL is temporary and has a very short time of life. When I pass it to another controller, and make API call, it doesn't work. But if I save the video directly in the delegate method of UIImagePickerViewController, then it works fine.

manonthemoon
  • 2,611
  • 8
  • 26
  • 40