gpt4 book ai didi

swift - 快速的 Alamofire 和内存警告

转载 作者:可可西里 更新时间:2023-11-01 01:43:16 25 4
gpt4 key购买 nike

我是 Alamofire 框架的新手。我尝试下载数据文件。代码是:

Alamofire.download(.GET, urlStr, { (temporaryURL, response) in
if let directoryURL = NSFileManager.defaultManager()
.URLsForDirectory(.DocumentDirectory,
inDomains: .UserDomainMask)[0]
as? NSURL {
let pathComponent = response.suggestedFilename

return directoryURL.URLByAppendingPathComponent(pathComponent!)
}

return temporaryURL
})

文件下载成功。但是所有过程都在使用内存。如您所见,问题是,如果我尝试下载大文件(我的意思是超过 50mb),我会收到 didReceiveMemoryWarning 并且应用程序会自行关闭。我怎么能防止呢?

在测试中,我尝试下载一部电影(大小为 220mb),在模拟器中,内存使用量上升到 500mb。当我尝试我的手机时。它在显示内存警告后自行关闭。

最佳答案

如果你想下载大文件,你可以考虑另一个名为TCBlobDownloadSwift 的库。通过 thibaultCha。它是 TCBlobDownload 的 Swift 版本,已针对 ~150MB 到 ~1.2GB 的文件(主要是视频)进行了测试。

它的用法和Alamofire类似:

import TCBlobDownloadSwift

// Here is a simple delegate implementing TCBlobDownloadDelegate.
class DownloadHandler: NSObject, TCBlobDownloadDelegate {
init() {}

func download(download: TCBlobDownload, didProgress progress: Float, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
println("\(progress*100)% downloaded")
}

func download(download: TCBlobDownload, didFinishWithError error: NSError?, atLocation location: NSURL?) {
println("file downloaded at \(location)")
}
}

let fileURL = NSURL(string: "http://some.huge/file.mp4")
let download = TCBlobDownloadManager.sharedInstance
.downloadFileAtURL(fileURL!, toDirectory: nil, withName: nil, andDelegate: DownloadHandler())

关于swift - 快速的 Alamofire 和内存警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26926587/

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