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

时间:2015-09-30 16:00:52

标签: ios swift video path photo-gallery

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

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

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

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

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

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

当我写writeVideoAtPathToSavedPhotosAlbum时,它会返回一个如下的网址:

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

它们都有效,但几天后停止工作。

1 个答案:

答案 0 :(得分:1)

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

$sql = "
    UPDATE oc_product AS p
    INNER JOIN oc_currency as c
    ON p.price2_currency_code=c.`code`
    SET p.price=case WHEN p.price2_currency_code='EUR' THEN p.price2*c.`value`
    WHEN p.price2_currency_code='USD' THEN p.price2*c.`value`
    ELSE p.price
    END";

我也错过了当你录制视频时,你必须使用以下方式获取网址:

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

但是当你得到视频时,你必须这样做:

let tempImageURL = info[UIImagePickerControllerMediaURL] as! NSURL!

希望它有所帮助!!