ios - 如何使用 Kingfisher(或 AlamofireImage)手动设置缓存中图像的最大期限并清除过期项目?

标签 ios swift caching alamofire kingfisher

我想为多个组使用多个缓存方案,例如: 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()}

最佳答案

翠鸟中,

您可以使用以下方法清除不同类型的缓存:

let cache = ImageCache(name: "shortlived_cache")

cache.clearMemoryCache()
cache.clearDiskCache()

要根据您的要求设置缓存大小和时间,您可以使用:

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

关于ios - 如何使用 Kingfisher(或 AlamofireImage)手动设置缓存中图像的最大期限并清除过期项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44260849/

相关文章:

ios - 为什么我不能使用 NSDateFormatter 将我的 NSDate 格式化为指定的时区?

swift - 任何对象?没有名为 'objectAtIndex' 的成员

java - 再次设置 AtomicBoolean

ios - 在 iOS 上从服务器缓存和加载数据

android - 强制登录 jQuery Mobile/Phonegap 应用程序重新启动

iphone - 使用蒙皮控件创建 iPhone 屏幕

ios - 使用 Parse-Server 时,Parse LocalDataStore 不起作用 - 'Method requires Pinning enabled.'

ios - 如何调试 iOS 9 扩展

c# - 使用 NHibernate 和 memcached 帮助进行二级缓存

android - 在 React Native 中从 php url 获取数据并保存到本地 sqlite