gpt4 book ai didi

快速保存下载的视频 NSURLSession

转载 作者:行者123 更新时间:2023-11-30 13:38:41 26 4
gpt4 key购买 nike

我需要从服务器下载视频并保存视频以供稍后观看。

所以我需要下载视频并将其使用自定义名称保存在应用程序的文件系统中,到目前为止我可以下载数据(我猜)但无法存储它。

不,我不想使用额外的扩展框架或其他什么。

@IBOutlet var progressView: ProgressView!

@IBOutlet var statusLabel: UILabel!

@IBOutlet var downloadButton: DownloadButton!

private var downloadTask: NSURLSessionDownloadTask?

@IBAction func downloadButtonPressed() {
if let downloadTask = downloadTask {
downloadTask.cancel()
statusLabel.text = ""
} else {
statusLabel.text = "Downloading video"
downloadButton.setTitle("Stop download", forState: .Normal)
createDownloadTask()
}
}



func createDownloadTask() {
//small mp4 video link : http://techslides.com/demos/sample-videos/small.mp4

let url = NSURL(string: "http://techslides.com/demos/sample-videos/small.mp4")!

let downloadRequest = NSMutableURLRequest(URL: url)
let session = NSURLSession(configuration: NSURLSessionConfiguration.defaultSessionConfiguration(), delegate: self, delegateQueue: NSOperationQueue.mainQueue())

downloadTask = session.downloadTaskWithRequest(downloadRequest)
downloadTask!.resume()
}

func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
let progress = Float(totalBytesWritten) / Float(totalBytesExpectedToWrite)
progressView.animateProgressViewToProgress(progress)
progressView.updateProgressViewLabelWithProgress(progress * 100)
progressView.updateProgressViewWith(Float(totalBytesWritten), totalFileSize: Float(totalBytesExpectedToWrite))
}


func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
statusLabel.text = "Download finished"
print(downloadTask.response.suggestedFilename) // Gives file name
resetView()
}

func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
if error != nil {
statusLabel.text = "Download failed"
} else {
statusLabel.text = "Download finished"
}
resetView()
}

func resetView() {
downloadButton.setTitle("Start download", forState: .Normal)
downloadTask!.cancel()
}

最佳答案

好吧,我改变了整个功能并修复了它。

func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) {
statusLabel.text = "Download finished"

print(downloadTask.response!.suggestedFilename!)

let documentsUrl = NSFileManager.defaultManager().URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask).first! as NSURL
// your destination file url
let destinationUrl = documentsUrl.URLByAppendingPathComponent(String(downloadTask.response!.suggestedFilename!))
print(destinationUrl)

if NSFileManager().fileExistsAtPath(destinationUrl.path!) {
print("The file already exists at path")
} else {

let data = NSData(contentsOfURL: (downloadTask.response!.URL)!)

if data!.writeToURL(destinationUrl, atomically: true) {
print("file saved")
} else {
print("error saving file")
}

}



let filePath = String(destinationUrl.path!)
var fileSize : UInt64 = 0

do {
let attr : NSDictionary? = try NSFileManager().attributesOfItemAtPath(filePath)

if let _attr = attr {
fileSize = _attr.fileSize();
print(fileSize)
self.statusLabel.text = String(fileSize)
self.performSegueWithIdentifier("video", sender: self)
}
} catch {
print("Error: \(error)")
}

resetView()
}

关于快速保存下载的视频 NSURLSession,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35817876/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com