PGD*_*Dev 8 caching image-caching ios swift kingfisher
我正在使用Kingfisher
库来下载和缓存图像.我在实施中面临一些问题:
图像是否缓存在内存和磁盘中?
是否有任何规定只在磁盘上缓存图像?
我已经阅读了多篇关于此的帖子,但找不到任何解决方案.
aya*_*aio 15
是的,Kingfisher在内存和磁盘上缓存图像.
默认情况下,将使用的RAM量甚至不受限制,您必须自己设置值:
ImageCache.default.maxMemoryCost = 1024 * 1024 * yourValue
Run Code Online (Sandbox Code Playgroud)
1024 * 1024 * yourValue
全球百万像素的成本在哪里(我知道这很奇怪,但它不是兆字节,它是百万像素,因为图像可能有不同的位深度等).
例如,在我的测试中,使用的最大RAM值1024 * 1024 * 500
在120MB到300MB之间波动.
顺便提一下,这也是你告诉Kingfisher永远不要使用RAM而只缓存到磁盘的方法:
ImageCache.default.maxMemoryCost = 1
Run Code Online (Sandbox Code Playgroud)
这将迫使Kingfisher仅使用磁盘缓存.
如何调试
首先要检查您是否在正确的缓存中设置了最大值.也许你确实创建了自定义缓存?我的示例是设置默认缓存的值,如果没有定义其他缓存,则使用该值.
您可能还想手动清除内存缓存并比较之前和之后的RAM占用:
ImageCache.default.clearMemoryCache()
Run Code Online (Sandbox Code Playgroud)
如果您认为某个大图像在内存缓存中不应该存在,您可以使用isImageCached
以下命令进行验证:
if let result = ImageCache.default.isImageCached(forKey: imageLink) {
print(result.cached)
print(result.cacheType)
}
Run Code Online (Sandbox Code Playgroud)
如果有人在寻找答案以明确下载图像并将其缓存而不使用imageView,则示例代码为:
ImageDownloader.default.downloadImage(with: imgUrl, retrieveImageTask: nil, options: [], progressBlock: nil) { (image, error, url, data) in
print("Downloaded Image: \(url)")
//cache image:
if let image = image, let url = url {
ImageCache.default.store(image, forKey: url.absoluteString)
}
}
Run Code Online (Sandbox Code Playgroud)
参考:https : //github.com/onevcat/Kingfisher/wiki/Cheat-Sheet
斯威夫特 5.3,Xcode 12
/sf/answers/3104808801/,最新版本如下
ImageCache.default.memoryStorage.config.totalCostLimit = 1 //1 in bytes
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
3909 次 |
最近记录: |