ios – Swift – 使用downloadTaskWithURL下载视频

前端之家收集整理的这篇文章主要介绍了ios – Swift – 使用downloadTaskWithURL下载视频前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在下载一个视频,感谢downloadTaskWithURL,我用这段代码将它保存到我的画廊:
  1. func saveVideoBis(fileStringURL:String){
  2.  
  3. print("saveVideoBis");
  4.  
  5. let url = NSURL(string: fileStringURL);
  6. (NSURLSession.sharedSession().downloadTaskWithURL(url!) { (location:NSURL?,r:NSURLResponse?,e:NSError?) -> Void in
  7.  
  8. let mgr = NSFileManager.defaultManager()
  9.  
  10. let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)[0];
  11.  
  12. print(documentsPath);
  13.  
  14. let destination = NSURL(string: NSString(format: "%@/%@",documentsPath,url!.lastPathComponent!) as String);
  15.  
  16. print(destination);
  17.  
  18. try? mgr.moveItemAtPath(location!.path!,toPath: destination!.path!)
  19.  
  20. PHPhotoLibrary.requestAuthorization({ (a:PHAuthorizationStatus) -> Void in
  21. PHPhotoLibrary.sharedPhotoLibrary().performChanges({
  22. PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(destination!);
  23. }) { completed,error in
  24. if completed {
  25. print(error);
  26. print("Video is saved!");
  27. self.sendNotification();
  28. }
  29. }
  30. })
  31. }).resume()
  32. }

它在我的模拟器上工作得非常好,但在我的iPad上,即使打印(“视频已保存!”),视频也不会保存;出现.
你知道为什么吗?

我的控制台中也出现了该消息

Unable to create data from file (null)

解决方法

请通过以下代码检查评论

Xcode 8•Swift 3

  1. import UIKit
  2. import Photos
  3.  
  4. class ViewController: UIViewController {
  5.  
  6. func downloadVideoLinkAndCreateAsset(_ videoLink: String) {
  7.  
  8. // use guard to make sure you have a valid url
  9. guard let videoURL = URL(string: videoLink) else { return }
  10.  
  11. guard let documentsDirectoryURL = FileManager.default.urls(for: .documentDirectory,in: .userDomainMask).first else { return }
  12.  
  13. // check if the file already exist at the destination folder if you don't want to download it twice
  14. if !FileManager.default.fileExists(atPath: documentsDirectoryURL.appendingPathComponent(videoURL.lastPathComponent).path) {
  15.  
  16. // set up your download task
  17. URLSession.shared.downloadTask(with: videoURL) { (location,response,error) -> Void in
  18.  
  19. // use guard to unwrap your optional url
  20. guard let location = location else { return }
  21.  
  22. // create a deatination url with the server response suggested file name
  23. let destinationURL = documentsDirectoryURL.appendingPathComponent(response?.suggestedFilename ?? videoURL.lastPathComponent)
  24.  
  25. do {
  26.  
  27. try FileManager.default.moveItem(at: location,to: destinationURL)
  28.  
  29. PHPhotoLibrary.requestAuthorization({ (authorizationStatus: PHAuthorizationStatus) -> Void in
  30.  
  31. // check if user authorized access photos for your app
  32. if authorizationStatus == .authorized {
  33. PHPhotoLibrary.shared().performChanges({
  34. PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: destinationURL)}) { completed,error in
  35. if completed {
  36. print("Video asset created")
  37. } else {
  38. print(error)
  39. }
  40. }
  41. }
  42. })
  43.  
  44. } catch { print(error) }
  45.  
  46. }.resume()
  47.  
  48. } else {
  49. print("File already exists at destination url")
  50. }
  51.  
  52. }
  53.  
  54. override func viewDidLoad() {
  55. super.viewDidLoad()
  56. downloadVideoLinkAndCreateAsset("https://www.yourdomain.com/yourmovie.mp4")
  57. }
  58.  
  59. }

猜你在找的iOS相关文章