ios – Swift – 使用downloadTaskWithURL下载视频

ios – Swift – 使用downloadTaskWithURL下载视频,第1张

概述我正在下载一个视频,感谢downloadTaskWithURL,我用这段代码将它保存到我的画廊: func saveVideoBis(fileStringURL:String){ print("saveVideoBis"); let url = NSURL(string: fileStringURL); (NSURLSession.sharedSession().dow 我正在下载一个视频,感谢downloadTaskWithURL,我用这段代码将它保存到我的画廊:
func saveVIDeoBis(@R_403_6852@StringURL:String){    print("saveVIDeoBis");    let url = NSURL(string: @R_403_6852@StringURL);    (NSURLSession.sharedSession().downloadTaskWithURL(url!) { (location:NSURL?,r:NSURLResponse?,e:NSError?) -> VoID in        let mgr = NS@R_403_6852@Manager.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.creationRequestForAssetFromVIDeoAt@R_403_6852@URL(destination!);                }) { completed,error in                    if completed {                        print(error);                        print("VIDeo is saved!");                        self.sendNotification();                    }            }        })    }).resume()}

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

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

Unable to create data from @R_403_6852@ (null)

解决方法 请通过以下代码检查评论:

Xcode 8•Swift 3

import UIKitimport Photosclass VIEwController: UIVIEwController {    func downloadVIDeolinkAndCreateAsset(_ vIDeolink: String) {        // use guard to make sure you have a valID url        guard let vIDeoURL = URL(string: vIDeolink) else { return }        guard let documentsDirectoryURL = @R_403_6852@Manager.default.urls(for: .documentDirectory,in: .userDomainMask).first else { return }        // check if the @R_403_6852@ already exist at the destination folder if you don't want to download it twice        if !@R_403_6852@Manager.default.@R_403_6852@Exists(atPath: documentsDirectoryURL.appendingPathComponent(vIDeoURL.lastPathComponent).path) {            // set up your download task            URLSession.shared.downloadTask(with: vIDeoURL) { (location,response,error) -> VoID in                // use guard to unwrap your optional url                guard let location = location else { return }                // create a deatination url with the server response suggested @R_403_6852@ name                let destinationURL = documentsDirectoryURL.appendingPathComponent(response?.suggested@R_403_6852@name ?? vIDeoURL.lastPathComponent)                do {                    try @R_403_6852@Manager.default.moveItem(at: location,to: destinationURL)                    PHPhotolibrary.requestAuthorization({ (authorizationStatus: PHAuthorizationStatus) -> VoID in                        // check if user authorized access photos for your app                        if authorizationStatus == .authorized {                            PHPhotolibrary.shared().performChanges({                                PHAssetChangeRequest.creationRequestForAssetFromVIDeo(at@R_403_6852@URL: destinationURL)}) { completed,error in                                    if completed {                                        print("VIDeo asset created")                                    } else {                                        print(error)                                    }                            }                        }                    })                } catch { print(error) }            }.resume()        } else {            print("@R_403_6852@ already exists at destination url")        }    }    overrIDe func vIEwDIDLoad() {        super.vIEwDIDLoad()        downloadVIDeolinkAndCreateAsset("https://www.yourdomain.com/yourmovIE.mp4")    }}
总结

以上是内存溢出为你收集整理的ios – Swift – 使用downloadTaskWithURL下载视频全部内容,希望文章能够帮你解决ios – Swift – 使用downloadTaskWithURL下载视频所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1109308.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存