我正在下载一个视频,感谢downloadTaskWithURL,我用这段代码将它保存到我的画廊:
func saveVideoBis(fileStringURL:String){
print("saveVideoBis");
let url = NSURL(string: fileStringURL);
(NSURLSession.sharedSession().downloadTaskWithURL(url!) { (location:NSURL?, r:NSURLResponse?, e:NSError?) -> Void in
let mgr = NSFileManager.defaultManager()
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0];
print(documentsPath);
let destination = NSURL(string: NSString(format: "%@/%@", documentsPath, url!.lastPathComponent!) as String);
print(destination);
try? mgr.moveItemAtPath(location!.path!, toPath: destination!.path!)
PHPhotoLibrary.requestAuthorization({ (a:PHAuthorizationStatus) -> Void in
PHPhotoLibrary.sharedPhotoLibrary().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(destination!);
}) { completed, error in
if completed {
print(error);
print("Video is saved!");
self.sendNotification();
}
}
})
}).resume()
}
Run Code Online (Sandbox Code Playgroud)
它在我的模拟器上工作得非常好,但在我的iPad上,即使print("Video is saved!");显示,视频也不会保存.你知道为什么吗?
我的控制台中也出现了该消息
无法从文件创建数据(null)
嗨我有一大堆.mp3文件我想与NSFileManager一起使用并存储在documents文件夹中.有没有办法我可以在线下载.mp3文件,然后将它保存到文档文件夹?这就是我用于本地文件的内容.
let filemanager = NSFileManager.defaultManager()
let documentsPath : AnyObject = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0]
let destinationPath:NSString = documentsPath.stringByAppendingString("/Attention.mp3")
if (!filemanager.fileExistsAtPath(destinationPath)) {
var theError: NSError?
let fileForCopy = NSBundle.mainBundle().pathForResource("Attention",ofType:"mp3")
filemanager.copyItemAtPath(fileForCopy!,toPath:destinationPath, error: &theError)
if (theError == nil) {
println("The music files has been saved.")
} else {
println("Error")
}
} else {
println("The files already exist")
}
Run Code Online (Sandbox Code Playgroud) 从iOS 10开始,Apple提供了下载HLS(m3u8)视频以供离线查看的支持。
我的问题是:是否只有在播放HLS时才可以下载?或者我们可以在用户按下下载按钮并显示进度时下载。
是否有人在Objective C版本中实现了此功能?实际上,我以前的应用程序是在Objective C中制作的。现在,我想增加对下载HLS而不是MP4的支持(以前我是为了离线查看而下载MP4的)。
我真的很绝望。请分享想法或任何实施的代码。