我是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并且app自己关闭了。我怎么能阻止这个?
在测试中我尝试下载一部电影(大小为220mb),在模拟器中,内存使用量高达500mb。当我尝试我的手机它在showin记忆警告后自行关闭。
答案 0 :(得分:2)
如果要下载大文件,可以考虑使用另一个名为TCBlobDownloadSwift的lib by thibaultCha。它是一个Swift版本的TCBlobDownload,经过大约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())