【问题标题】:How to set max period on image in cache and clear expired items manually using Kingfisher(or AlamofireImage)?如何使用 Kingfisher(或 AlamofireImage)在缓存中设置图像的最大期限并手动清除过期项目?
【发布时间】:2017-10-30 20:18:58
【问题描述】:

我想为多个组使用多个缓存方案,例如: 1-此图像组应每 60 秒刷新一次。 2-除非出现内存警告,否则该图像组应该永远存在。 我不知道如何用一个像 AlamofireImage(或 Kingfisher)这样的库来实现多个缓存程序。我写了这段代码,但它无法清除文件夹中过期的图像(我不想清除所有缓存文件夹内容):

let downloader = ImageDownloader(name: "shortlived_image_downloader")
let cache = ImageCache(name: "shortlived_cache")
cache.maxCachePeriodInSecond = 60
cell.onPlayingImageView.kf.setImage(with: url,
                                                  placeholder: UIImage(named:"Placeholder_S"),
                                                  options: [.transition(ImageTransition.fade(0.25)),
                                                            .downloader(downloader),
                                                            .targetCache(cache)],
                                                  progressBlock: nil,
                                                  completionHandler: nil)

func clearKFShortLiveCache() {
         let cache = ImageCache(name: "shortlived_cache")
         cache.clearMemoryCache()
         cache.cleanExpiredDiskCache()}

【问题讨论】:

    标签: ios swift caching alamofire kingfisher


    【解决方案1】:

    翠鸟中,

    您可以使用以下方法清除不同类型的cache

    let cache = ImageCache(name: "shortlived_cache")
    
    cache.clearMemoryCache()
    cache.clearDiskCache()
    

    要根据你的要求设置cache size and time,你可以使用:

    cache.maxMemoryCost = YOUR_VALUE
    cache.maxCachePeriodInSecond = YOUR_VALUE
    cache.maxDiskCacheSize = YOUR_VALUE
    

    除非出现内存警告,否则图像组应该永远存在:它由Kingfisher自动处理。

    AutoPurgingImageCache 概念不由Kingfisher 处理。 AlamofireImage 支持这一点。在Kingfisher,你必须自己根据last access date来处理auto purging

    关于这些属性和方法的详细介绍,可以参考:

    https://github.com/onevcat/Kingfisher/wiki/Cheat-Sheet http://cocoadocs.org/docsets/Kingfisher/1.1.2/Classes/ImageCache.html

    【讨论】:

      猜你喜欢
      • 2017-09-18
      • 2023-03-15
      • 1970-01-01
      • 2016-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-18
      • 1970-01-01
      相关资源
      最近更新 更多