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");
})
}
})
}
I'va也会考虑这个:
I'va also look into this :
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)
但是我有错误无法从文件创建数据(null)。我的assetChangeRequest是零。我不明白,因为我的网址是有效的,当我使用浏览器访问它时,它会下载一个快速时间文件。
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屋!