Swift - 从远程 URL 下载视频并将其保存在相册中 [英] Swift - Download a video from distant URL and save it in an photo album
问题描述
我目前正在我的应用中显示一个视频,我希望用户能够将其保存到其设备画廊/相册照片/相机胶卷中.这是我正在做的,但视频未保存在相册中:/
I'm currently displaying a video in my app and I want the user to be able to save it to its device gallery/album photo/camera roll. Here it's what I'm doing but the video is not saved in the album :/
func downloadVideo(videoImageUrl:String)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
//All stuff here
print("downloadVideo");
let url=NSURL(string: videoImageUrl);
let urlData=NSData(contentsOfURL: url!);
if((urlData) != nil)
{
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0];
let fileName = videoImageUrl; //.stringByDeletingPathExtension
let filePath="(documentsPath)/(fileName)";
//saving is done on main thread
dispatch_async(dispatch_get_main_queue(), { () -> Void in
urlData?.writeToFile(filePath, atomically: true);
print("videoSaved");
})
}
})
}
我也在研究这个:
let url:NSURL = NSURL(string: fileURL)!;
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
let assetChangeRequest = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(url);
let assetPlaceHolder = assetChangeRequest!.placeholderForCreatedAsset;
let albumChangeRequest = PHAssetCollectionChangeRequest(forAssetCollection: self.assetCollection)
albumChangeRequest!.addAssets([assetPlaceHolder!])
}, completionHandler: saveVideoCallBack)
但我有错误无法从文件(空)创建数据".我的资产变更请求"为零.我不明白,因为我的 url 是有效的,当我使用浏览器访问它时,它会下载一个快速时间文件.
But I have the error "Unable to create data from file (null)". My "assetChangeRequest" is nil. I don't understand as my url is valid and when I go to it with a browser, it download a quick time file.
如果有人可以帮助我,将不胜感激!我正在使用 Swift 并以 iOS 8.0 为目标.
If anyone can help me, it would be appreciated ! I'm using Swift and targeting iOS 8.0 min.
推荐答案
更新
想使用 URLSession 更新 Swift 3 的答案,并发现相关主题中已经存在答案这里.使用它.
Wanted to update the answer for Swift 3 using URLSession and figured out that the answer already exists in related topic here. Use it.
原答案
下面的代码将视频文件保存到相机胶卷.我重用了你的代码,做了一个小改动 - 我删除了 let fileName = videoImageUrl;
因为它会导致不正确的文件路径.
The code below saves a video file to Camera Roll. I reused your code with a minor change - I removed let fileName = videoImageUrl;
because it leads to incorrect file path.
我测试了此代码并将资产保存到相机胶卷中.您询问要在 creationRequestForAssetFromVideoAtFileURL
中放入什么内容 - 如下例所示放入下载的视频文件的链接.
I tested this code and it saved the asset into camera roll. You asked what to place into creationRequestForAssetFromVideoAtFileURL
- put a link to downloaded video file as in the example below.
let videoImageUrl = "http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_1mb.mp4"
DispatchQueue.global(qos: .background).async {
if let url = URL(string: urlString),
let urlData = NSData(contentsOf: url) {
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0];
let filePath="(documentsPath)/tempFile.mp4"
DispatchQueue.main.async {
urlData.write(toFile: filePath, atomically: true)
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: URL(fileURLWithPath: filePath))
}) { completed, error in
if completed {
print("Video is saved!")
}
}
}
}
}
这篇关于Swift - 从远程 URL 下载视频并将其保存在相册中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!