I have an app in which the user can record a video, this video is saved in the photo gallery, and I store the path to the video so that in the future the user could see again the video inside the app. The problem is that the method that I use I think it's giving me some kind of temporary path, and after some days, the video still in the gallery, but the path is not valid anymore and make the app crash. This is the code I'm using:
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
let mediaType: String = info["UIImagePickerControllerMediaType"] as! String
if mediaType == "public.movie" {
let tempImageURL = info[UIImagePickerControllerMediaURL] as! NSURL!
let pathString = tempImageURL.relativeString
self.dismissViewControllerAnimated(true, completion: {})
if picker.sourceType == UIImagePickerControllerSourceType.PhotoLibrary {
self.videoPath = pathString
// Save the path in the DB
} else {
VideoManager.saveVideo(tempImageURL, onComplete: { (path) -> Void in
self.videoPath = path
// Save the path in the DB
})
var fileManager: NSFileManager = NSFileManager()
fileManager.removeItemAtPath(pathString!, error: nil)
}
}
}
And the VideoManager.saveVideo method code is the following:
func saveVideo(videoURL: NSURL, onComplete:((path: String) -> Void)) {
var assetsLibrary: ALAssetsLibrary = ALAssetsLibrary()
assetsLibrary.writeVideoAtPathToSavedPhotosAlbum(videoURL, completionBlock: { (assetURL: NSURL!, error: NSError!) -> Void in
var path: String = error == nil ? "\(assetURL)" : kEmptyString
onComplete(path: path)
})
}
I don't know what I'm doing wrong, I've tried with the method UISaveVideoAtPathToSavedPhotosAlbum but without success.. Any ideas?
For giving a little more information, when the video is selected from the gallery, the url I get is like the following one:
file:///private/var/mobile/Containers/Data/Application/D2E8E31B-CEA0-43B0-8EF9-1820F6BDE4A9/tmp/trim.AD855155-AB78-4A16-9AA8-DF2B3F39824E.MOV
And when I record a new video using the camera, first I have this URL:
file:///private/var/mobile/Containers/Data/Application/D2E8E31B-CEA0-43B0-8EF9-1820F6BDE4A9/tmp/capture/capturedvideo.MOV
and when I do writeVideoAtPathToSavedPhotosAlbum it returns an URL like:
assets-library://asset/asset.MOV?id=958507B5-1353-4DDC-BC07-D9CBC6126657&ext=MOV
Both of them work, but some days later stop working.