如何清除使用 Kingfisherin UITableView 加载的图像的内存和磁盘缓存?
How to clear memory and disk cache for images loaded using Kingfisherin UITableView?
我需要释放使用 Kingfisher 获取的图像占用的所有内存。我有一个存储大量图像的 UITableView 并且还具有加载更多功能。
我尝试了这些措施。
我在 viewDidLoad() 中设置缓存大小。
let cache = KingfisherManager.sharedManager.cache
cache.maxMemoryCost = 50 * 1024 * 1024
// Set max disk cache to 50 mb. Default is no limit.
cache.maxDiskCacheSize = 50 * 1024 * 1024
// Set max disk cache to duration to 3 days, Default is 1 week.
cache.maxCachePeriodInSecond = 60 * 60 * 24 * 3
在 viewWillDisappear() 中,我正在清除它。
cache.clearMemoryCache()
// Clear disk cache.
cache.clearDiskCache()
// Clean expired or size exceeded disk cache.
cache.cleanExpiredDiskCache()
仍然没有像预期的那样释放内存。如果我遗漏了什么,请纠正我。
收到内存警告后,我清除了缓存,现在它对我来说工作正常:
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
cache.clearMemoryCache()
cache.clearDiskCache()
cache.cleanExpiredDiskCache()
}
Swift4 的更新:
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
KingfisherManager.shared.cache.clearMemoryCache()
KingfisherManager.shared.cache.clearDiskCache()
KingfisherManager.shared.cache.cleanExpiredDiskCache()
}
根据 documentation:
Kingfisher will purge the memory cache when received a memory warning, as well as clean the expired and size exceeded cache when needed. Normally there is no need to clean cache yourself. These methods exist in case of you want your users have more control on the cache.
翠鸟 Swift 4
KingfisherManager.shared.cache.clearMemoryCache()
KingfisherManager.shared.cache.clearDiskCache()
KingfisherManager.shared.cache.cleanExpiredDiskCache()
Swift 5,你可以把3行换成1行
KingfisherManager.shared.cache.clearCache()
手动从缓存中删除图像
Kingfisher 自动管理其缓存。但是您仍然可以从缓存中手动删除某个图像:
KingfisherManager.shared.default.cache.removeImage(forKey: cacheKey)
或者,通过更多控制:
KingfisherManager.shared.default.cache.removeImage(
forKey: cacheKey,
processorIdentifier: processor.identifier,
fromMemory: false,
fromDisk: true)
{
print("Removed!")
}
只需加载选项:[.forceRefresh]
如果设置,Kingfisher 将忽略缓存并尝试为图像源启动下载任务。
self.kf.setImage(with: url, placeholder: placeHolderImage, options: [.forceRefresh])
我需要释放使用 Kingfisher 获取的图像占用的所有内存。我有一个存储大量图像的 UITableView 并且还具有加载更多功能。
我尝试了这些措施。
我在 viewDidLoad() 中设置缓存大小。
let cache = KingfisherManager.sharedManager.cache
cache.maxMemoryCost = 50 * 1024 * 1024
// Set max disk cache to 50 mb. Default is no limit.
cache.maxDiskCacheSize = 50 * 1024 * 1024
// Set max disk cache to duration to 3 days, Default is 1 week.
cache.maxCachePeriodInSecond = 60 * 60 * 24 * 3
在 viewWillDisappear() 中,我正在清除它。
cache.clearMemoryCache()
// Clear disk cache.
cache.clearDiskCache()
// Clean expired or size exceeded disk cache.
cache.cleanExpiredDiskCache()
仍然没有像预期的那样释放内存。如果我遗漏了什么,请纠正我。
收到内存警告后,我清除了缓存,现在它对我来说工作正常:
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
cache.clearMemoryCache()
cache.clearDiskCache()
cache.cleanExpiredDiskCache()
}
Swift4 的更新:
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
KingfisherManager.shared.cache.clearMemoryCache()
KingfisherManager.shared.cache.clearDiskCache()
KingfisherManager.shared.cache.cleanExpiredDiskCache()
}
根据 documentation:
Kingfisher will purge the memory cache when received a memory warning, as well as clean the expired and size exceeded cache when needed. Normally there is no need to clean cache yourself. These methods exist in case of you want your users have more control on the cache.
翠鸟 Swift 4
KingfisherManager.shared.cache.clearMemoryCache()
KingfisherManager.shared.cache.clearDiskCache()
KingfisherManager.shared.cache.cleanExpiredDiskCache()
Swift 5,你可以把3行换成1行
KingfisherManager.shared.cache.clearCache()
手动从缓存中删除图像
Kingfisher 自动管理其缓存。但是您仍然可以从缓存中手动删除某个图像:
KingfisherManager.shared.default.cache.removeImage(forKey: cacheKey)
或者,通过更多控制:
KingfisherManager.shared.default.cache.removeImage(
forKey: cacheKey,
processorIdentifier: processor.identifier,
fromMemory: false,
fromDisk: true)
{
print("Removed!")
}
只需加载选项:[.forceRefresh]
如果设置,Kingfisher 将忽略缓存并尝试为图像源启动下载任务。
self.kf.setImage(with: url, placeholder: placeHolderImage, options: [.forceRefresh])