8

我想使用 AlamofireImage 从 url 下载图像,然后将其缓存在设备磁盘空间中。我读了几篇关于这个的帖子,发现 AlamofireImage 在ImageDownloader类的帮助下支持这个。这个 SO answer给出了最有趣的信息

所以我尝试为 ImageDownloader 设置一个自定义的 NSURLCache ,以便它将图像直接缓存到磁盘。我通过将内存容量设置为 0 来做到这一点。然后我使用这个自定义 ImageDownloader 来下载图像。我为磁盘路径指定的文件夹是在磁盘上创建的,但不幸的是它保持为空,并且图像没有以任何方式缓存。

** 编辑:** 重要的是要注意缓存的响应不是保存在缓存目录中的文件夹中,而是保存在文件夹旁边的数据库文件中。

谁能告诉我我在这里做错了什么?非常感谢您的阅读!

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
        }
    }
}
4

2 回答 2

5

我不是为了代表做这个。只是为了节省您使用编译器的时间。

Swift 3.0中

class DiskCache: URLCache {


  private let constSecondsToKeepOnDisk = numDaysToKeep*24*60*60 // numDaysToKeep is your own constant or hard-coded value

  override func storeCachedResponse(_ cachedResponse: CachedURLResponse, for request: URLRequest) {

    var customCachedResponse = cachedResponse
    // Set custom Cache-Control for Image-Response
    let response = cachedResponse.response as! HTTPURLResponse

    if let contentType = response.allHeaderFields["Content-Type"] as? String,
        var newHeaders = response.allHeaderFields as? [String: String], contentType.contains("image") {
        newHeaders["Cache-Control"] = "public, max-age=\(constSecondsToKeepOnDisk)"
        if let url = response.url, let newResponse = HTTPURLResponse(url: url, statusCode: response.statusCode, httpVersion: "HTTP/1.1", headerFields: newHeaders) {
            customCachedResponse = CachedURLResponse(response: newResponse, data: cachedResponse.data, userInfo: cachedResponse.userInfo, storagePolicy: cachedResponse.storagePolicy)
        }
    }
    super.storeCachedResponse(customCachedResponse, for: request)
  }
}
于 2016-12-16T09:41:01.703 回答
4

为了达到您的目标,让您NSURLCache使用的 diskCache 真正自定义为存储的图像设置您自己的到期日期:

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而不是每次都可以重用共享实例来调用方法时创建一个新的downloadImageUIImageView.af_sharedImageDownloader.downloadImage(URLRequest: request)

于 2016-10-27T13:07:05.950 回答