如何使用Kingfisher仅将图像缓存在磁盘中? [英] How to cache images only in disk using Kingfisher?
问题描述
我正在使用 Kingfisher
库下载和缓存图像.我在实施过程中遇到了一些问题:
I am using Kingfisher
library for downloading and caching images. I am facing some issues in the implementation:
-
图像是否同时缓存在内存和磁盘中?
Are the images cached in both memory and disk?
是否有仅在磁盘上缓存图像的规定?
Is there any provision to cache images only on disk?
我已经阅读了许多与此相关的文章,但是找不到任何解决方案.
I have already read multiple posts regarding this, but couldn't find any solution.
推荐答案
是的,Kingfisher在内存和磁盘上都缓存了图像.
Yes, Kingfisher caches images both in memory and on disk.
默认情况下,将使用的RAM数量甚至没有限制,您必须自己设置该值:
By default, the amount of RAM that will be used is not even limited, you have to set the value yourself:
ImageCache.default.maxMemoryCost = 1024 * 1024 * yourValue
其中1024 * 1024 * yourValue
是百万像素的全球成本(我知道这很奇怪,但不是兆字节,而是百万像素,因为图像可以具有不同的位深等).
where 1024 * 1024 * yourValue
is the global cost in megapixels (I know this is weird, but it's not megabytes, it's megapixels, because images can have different bit depth, etc).
例如,在我的测试中,使用的最大RAM值为1024 * 1024 * 500
在120MB和300MB之间波动.
For example, in my tests, the maximum RAM used with a value of 1024 * 1024 * 500
fluctuates between 120MB and 300MB.
顺便说一句,这也是告诉Kingfisher不要使用RAM而是仅缓存到磁盘的方法:
Incidentally, this is also how you tell Kingfisher to never use the RAM and only cache to disk:
ImageCache.default.maxMemoryCost = 1
这将迫使Kingfisher仅使用磁盘缓存.
This will force Kingfisher to only use the disk cache.
如何调试
第一件事是检查您是否在正确的缓存上设置了最大值.也许您确实创建了自定义缓存?我的示例是为默认缓存设置值,如果未定义其他值,则使用该值.
The first thing is to check that you're setting the max value on the right cache. Maybe you did create a custom cache? My example is setting the value for the default cache, the one used if no other is defined.
您可能还想手动清除内存缓存并比较之前和之后的RAM占用情况:
You may also want to manually clear the memory cache and compare the RAM occupation before and after:
ImageCache.default.clearMemoryCache()
如果您认为不应该在内存缓存中保存一些大图像,则可以使用isImageCached
进行验证:
If you think that some big image is in the memory cache when it shouldn't be, you can verify with isImageCached
:
if let result = ImageCache.default.isImageCached(forKey: imageLink) {
print(result.cached)
print(result.cacheType)
}
这篇关于如何使用Kingfisher仅将图像缓存在磁盘中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!