Swift:将视频从 NSURL 保存到用户相机胶卷 [英] Swift: save video from NSURL to user camera roll

查看:21
本文介绍了Swift:将视频从 NSURL 保存到用户相机胶卷的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 NSURL 类型的变量 videoURL.

I have a variable videoURL of type NSURL.

如果我调用 println(videoURL) 它将返回如下内容:http://files.parsetfss.com/d540f71f-video.mp4

If I call println(videoURL) it would return something like this: http://files.parsetfss.com/d540f71f-video.mp4

我设置了一个按钮,可以获取此 videoURL 并将视频保存到用户的相机胶卷中.

I have a button set up that should take this videoURL and save the video to the user's camera roll.

我做得最好的是:

UISaveVideoAtPathToSavedPhotosAlbum(videoPath: String!, completionTarget: AnyObject!, completionSelector: Selector, contextInfo: UnsafeMutablePointer<Void>)

虽然我什至不确定这是否可行,但我不知道如何将 videoFile:NSURL 转换为 videoPath.

While I'm not even sure if this will work or not, I can't figure out how to convert videoFile:NSURL into a videoPath.

对此的任何帮助表示赞赏.

Any help is appreciated on this.

以下不成功:

UISaveVideoAtPathToSavedPhotosAlbum(videoURL.relativePath, self, nil, nil)

推荐答案

AssetsLibrary 已弃用

AssetsLibrary is deprecated

1:导入照片

import Photos

2:使用此代码将视频从 url 保存到相机库.

2: Use this code to save video from url to camera library.

PHPhotoLibrary.sharedPhotoLibrary().performChanges({
             PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(nsUrlToYourVideo)
         }) { saved, error in
             if saved {
                 let alertController = UIAlertController(title: "Your video was successfully saved", message: nil, preferredStyle: .Alert) 
                 let defaultAction = UIAlertAction(title: "OK", style: .Default, handler: nil)
                 alertController.addAction(defaultAction)
                 self.presentViewController(alertController, animated: true, completion: nil)
             }
         }

斯威夫特 3 &斯威夫特 4

PHPhotoLibrary.shared().performChanges({
    PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: urlToYourVideo)
}) { saved, error in
    if saved {
        let alertController = UIAlertController(title: "Your video was successfully saved", message: nil, preferredStyle: .alert)
        let defaultAction = UIAlertAction(title: "OK", style: .default, handler: nil)
        alertController.addAction(defaultAction)
        self.present(alertController, animated: true, completion: nil)
    }
}

这篇关于Swift:将视频从 NSURL 保存到用户相机胶卷的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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