Save video on the gallery and store the path to the video

末鹿安然 提交于 2019-12-24 13:33:39

问题


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.


回答1:


OK finally I found the solution, the thing was that you can't access directly the photo gallery with the stored url, you got to use the assetLibrary.assetForURL method, which I was missing. In the end the imagepickercontroller delegate method is like this:

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
    let mediaType: String = info["UIImagePickerControllerMediaType"] as! String
    if mediaType == "public.movie" {
        self.dismissViewControllerAnimated(true, completion: {})
        if picker.sourceType == UIImagePickerControllerSourceType.PhotoLibrary {
            let tempImageURL = info[UIImagePickerControllerReferenceURL] as! NSURL!
            self.videoPath = tempImageURL.absoluteString
        } else {
            let tempImageURL = info[UIImagePickerControllerMediaURL] as! NSURL!
            VideoManager.saveVideo(tempImageURL, onComplete: { (path) -> Void in
                self.videoPath = path
            })
        }
    } 
}

I also was missing that when you record a video, you got to obtain the url using:

let tempImageURL = info[UIImagePickerControllerMediaURL] as! NSURL!

But when you get the video you have to do:

let tempImageURL = info[UIImagePickerControllerReferenceURL] as! NSURL!

Hope it helps!!



来源:https://stackoverflow.com/questions/32870711/save-video-on-the-gallery-and-store-the-path-to-the-video

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!