将视频保存在图库中并存储视频的路径

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

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

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方法代码如下:

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 方法但没有成功。有什么想法吗?

为了提供更多信息,当从图库中选择视频时,我得到的 url 如下所示:

file:///private/var/mobile/Containers/Data/Application/D2E8E31B-CEA0-43B0-8EF9-1820F6BDE4A9/tmp/trim.AD855155-AB78-4A16-9AA8-DF2B3F39824E.MOV

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

file:///private/var/mobile/Containers/Data/Application/D2E8E31B-CEA0-43B0-8EF9-1820F6BDE4A9/tmp/capture/capturedvideo.MOV

当我写 VideoAtPathToSavedPhotosAlbum 时,它 returns 一个 URL 比如:

assets-library://asset/asset.MOV?id=958507B5-1353-4DDC-BC07-D9CBC6126657&ext=MOV

他们都工作,但几天后停止工作。

好吧,我终于找到了解决方案,问题是你不能直接访问存储了 url 的照片库,你必须使用 assetLibrary.assetForURL 方法,我没有找到.最后imagepickercontroller委托方法是这样的:

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

我还遗漏了当您录制视频时,您必须使用以下方式获取 url:

let tempImageURL = info[UIImagePickerControllerMediaURL] as! NSURL!

但是当你得到视频后你必须做的:

let tempImageURL = info[UIImagePickerControllerReferenceURL] as! NSURL!

希望对您有所帮助!!