将视频保存在图库中并存储视频的路径 [英] Save video on the gallery and store the path to the video

查看:305
本文介绍了将视频保存在图库中并存储视频的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,用户可以在其中录制视频,该视频保存在照片库中,并且存储该视频的路径,以便将来用户可以再次在该应用程序中看到该视频.问题是我使用的方法给了我一种临时路径,几天后,视频仍在图库中,但是该路径不再有效,导致应用崩溃.这是我正在使用的代码:

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)
        }
    }
}

VideoManager.saveVideo方法代码如下:

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)
    })
}

我不知道自己在做什么错,我尝试使用UISaveVideoAtPathToSavedPhotosAlbum方法,但没有成功..有什么想法吗?

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

当我使用相机录制新视频时,首先我有以下URL:

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

当我写writeVideoAtPathToSavedPhotosAlbum时,它返回一个URL,如:

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.

推荐答案

好,终于找到了解决方案,问题是您无法使用存储的URL直接访问照片库,必须使用assetLibrary.assetForURL方法,这是我所缺少的.最后,imagepickercontroller委托方法是这样的:

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!

希望有帮助!

这篇关于将视频保存在图库中并存储视频的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆