Swift:将视频从NSURL保存到用户相机胶卷 [英] Swift: save video from NSURL to user camera roll
问题描述
我有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
.
对此有任何帮助。
修改:
以下是不成功的:
UISaveVideoAtPathToSavedPhotosAlbum(videoURL.relativePath, self, nil, nil)
推荐答案
不推荐使用AssetsLibrary
AssetsLibrary is deprecated
1:导入照片
import Photos
2:使用此代码将视频从网址保存到相机库。
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)
}
}
Swift 3& Swift 4
Swift 3 & Swift 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屋!