AlamofireImage磁盘缓存不起作用 [英] AlamofireImage Disk Cache not working
问题描述
我想使用AlamofireImage从URL下载图像,然后将其缓存在设备磁盘空间上。我阅读了有关此内容的几篇文章,发现AlamofireImage在 ImageDownloader 类。在此SO答案中,
I want to use AlamofireImage to download images from an url and then cache it on the device disk space. I read several posts about this and found out that AlamofireImage supports this with the help of the ImageDownloader class. The most interesting information was given in this SO answer
所以我尝试为ImageDownloader设置自定义NSURLCache,以便它将图像直接缓存到磁盘。我通过将内存容量设置为0来做到这一点。然后使用此自定义ImageDownloader下载图像。我为磁盘路径指定的文件夹是在磁盘上创建的,但不幸的是,该文件夹保持为空,并且图像没有以任何方式进行缓存。
So I tried to set a custom NSURLCache for the ImageDownloader so that it would cache the images directly to the disk. I did that by setting the memory capacity to 0. Then I use this custom ImageDownloader to download an image. The folder that I specified for the disk path is created on the disk but unfortunately it stays empty and the image is not cached in any way.
**编辑:**重要的是要注意,缓存的响应不是保存在缓存目录的文件夹中,而是保存在该文件夹旁边的数据库文件中。
** ** It is important to notice that the cached responses are not saved in the folder in the caches directory but in a database file next to the folder.
有人可以告诉我我这里做错了吗?非常感谢您的阅读!
Can anybody tell me what I am doing wrong here? Thanks very much for reading!
func diskImageDownloader(diskSpaceMB: Int = 100) -> ImageDownloader {
let diskCapacity = diskSpaceMB * 1024 * 1024
let diskCache = NSURLCache(memoryCapacity: 0, diskCapacity: diskCapacity, diskPath: "alamofireimage_disk_cache")
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
configuration.URLCache = diskCache
let downloader = ImageDownloader(configuration: configuration)
UIImageView.af_sharedImageDownloader = downloader
return downloader
}
func getProfileImage(atURL url: String, onComplete: SRServiceResponse<UIImage> -> Void) {
guard let imageURL = NSURL(string: url) else
{
// TODO: Fail here
return
}
let request = NSURLRequest(URL: imageURL)
let imageDownloader = self.diskImageDownloader()
imageDownloader.downloadImage(URLRequest: request) { (response) in
switch response.result
{
case .Success(let image):
// Do something
case .Failure(let error):
// Do something
}
}
}
推荐答案
要实现目标,请使 NSURLCache
用作磁盘缓存,实际上是自定义设置存储图像的到期日期:
To reach your goal make your NSURLCache
which you use as diskCache really custom to set your own expiration date for the stored images:
class DiskCache: NSURLCache {
private let constSecondsToKeepOnDisk = 30*24*60*60 // 30 days
override func storeCachedResponse(cachedResponse: NSCachedURLResponse, forRequest request: NSURLRequest) {
var customCachedResponse = cachedResponse
// Set custom Cache-Control for Image-Response
if let response = cachedResponse.response as? NSHTTPURLResponse,
let contentType = response.allHeaderFields["Content-Type"] as? String,
var newHeaders = response.allHeaderFields as? [String: String] where contentType.containsString("image") {
newHeaders["Cache-Control"] = "public, max-age=\(constSecondsToKeepOnDisk)"
if let url = response.URL, newResponse = NSHTTPURLResponse(URL: url, statusCode: response.statusCode, HTTPVersion: "HTTP/1.1", headerFields: newHeaders) {
customCachedResponse = NSCachedURLResponse(response: newResponse, data: cachedResponse.data, userInfo: cachedResponse.userInfo, storagePolicy: cachedResponse.storagePolicy)
}
}
super.storeCachedResponse(customCachedResponse, forRequest: request)
}
}
而不是每次重新使用共享实例时都创建新的 ImageDownloader
调用 downloadImage
方法: UIImageView.af_sharedImageDownloader.downloadImage(URLRequest:request)
Instead of creating a new ImageDownloader
every time you could reuse the shared instance to call the downloadImage
method: UIImageView.af_sharedImageDownloader.downloadImage(URLRequest: request)
这篇关于AlamofireImage磁盘缓存不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!