使用磁盘缓存图像(如果存在于Alamofire图像中) [英] Using disk cached images if present in Alamofire Images

查看:252
本文介绍了使用磁盘缓存图像(如果存在于Alamofire图像中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 NSURLCache as diskCache where您必须在 cachedResponse / NSURLCache_Class /#// apple_ref / occ / instm / NSURLCache / storeCachedResponse:forRequest:rel =nofollow noreferrer> storeCachedResponse 方法。顺便说一句,内存缓存由 ImageDownloader 中的 AutoPurgingImageCache 处理。
使用此方法设置下载程序

  func diskImageDownloader(diskSpaceMB:Int = 150) - > ImageDownloader {

let diskCapacity = diskSpaceMB * 1024 * 1024
let diskCache = NSURLCache(memoryCapacity:0,diskCapacity:diskCapacity,diskPath:image_disk_cache)
let configuration = NSURLSessionConfiguration。 defaultSessionConfiguration()
configuration.URLCache = diskCache
let downloader = ImageDownloader(configuration:configuration)
UIImageView.af_sharedImageDownloader = downloader
return downloader
}

更新于08/09/16 @ kishorer747:



NSURLCache memoryCapacity 为零,因为我不希望将图像请求响应保存在缓存中以节省内存。应该只在内存中通过 AutoPurgingImageCache 将请求网址保存为关键图像。您可以修改我的示例方法,为图像内存缓存设置所需的 cacheCapacity cachePurgeCapacity

 让cacheCapacity = 100 * 1024 * 1024 
让cachePurgeCapacity = 60 * 1024 * 1024
let imageCache:ImageRequestCache = AutoPurgingImageCache(memoryCapacity:cacheCapacity,preferredMemoryUsageAfterPurge:cachePurgeCapacity)
let downloader = ImageDownloader(configuration:configuration,imageCache:imageCache)


I'm using the AlamofireImage library to cache downloaded images.

Code:

import UIKit
import AlamofireImage

class ViewController: UIViewController {

    @IBOutlet weak var firstImageView: UIImageView!
    @IBOutlet weak var secondImageView: UIImageView!

    let downloader = ImageDownloader()
    let URLRequest = NSURLRequest(URL: NSURL(string: "https://httpbin.org/image/jpeg")!)

    override func viewDidLoad() {
        super.viewDidLoad()
        requestFirstImage()
    }

    func requestFirstImage() {
        downloader.downloadImage(URLRequest: URLRequest) { response in
            print(response.request)
            print(response.response)
            debugPrint(response.result)
            if let image = response.result.value {
                self.firstImageView.image = image
                self.requestSecondImage()
            }
        }
    }

    func requestSecondImage() {
        downloader.downloadImage(URLRequest: URLRequest) { response in
            print(response.request)
            print(response.response)
            debugPrint(response.result)
            if let image = response.result.value {
                self.secondImageView.image = image
            }
        }
    }
}

Log:

As the log shows the first image is requested and the second one is fetched from the cache. No extra request is made and the image shows instantly.

I expect when i re-launch the app that even the first image where fetched from the cache but the Log remains the same. I looked to the Library/Caches/.../fsCachedData and the image is there, ready to be fetched.

Question: What i'm missing here ? I need that the first image get fetched from the disk cache on subsequent requests.

解决方案

This approach saves the image requests as long on the disk as their cache control max age says and space is available. If you want to set an own max age you have to set up a custom NSURLCache as diskCache where you have to return your modified cachedResponse in the storeCachedResponse method. By the way, the memory cache is handled by the AutoPurgingImageCache in the ImageDownloader. Set up your downloader with this method:

func diskImageDownloader(diskSpaceMB: Int = 150) -> ImageDownloader {

        let diskCapacity = diskSpaceMB * 1024 * 1024
        let diskCache = NSURLCache(memoryCapacity: 0, diskCapacity: diskCapacity, diskPath: "image_disk_cache")
        let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
        configuration.URLCache = diskCache
        let downloader = ImageDownloader(configuration: configuration)
        UIImageView.af_sharedImageDownloader = downloader
        return downloader
    }

Updated on 08/09/16 for @kishorer747:

The NSURLCache memoryCapacity is zero because I don't want image request responses to be saved in cache to save memory. There should only the image be saved for the request url as key by the AutoPurgingImageCache in memory. You can modify my example method as followed to set your desired cacheCapacity and cachePurgeCapacity for the image memory cache:

let cacheCapacity = 100 * 1024 * 1024
let cachePurgeCapacity = 60 * 1024 * 1024
let imageCache: ImageRequestCache = AutoPurgingImageCache(memoryCapacity: cacheCapacity, preferredMemoryUsageAfterPurge: cachePurgeCapacity)
let downloader = ImageDownloader(configuration: configuration, imageCache: imageCache)

这篇关于使用磁盘缓存图像(如果存在于Alamofire图像中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆