Kingfisher: Can't set maximum cache size of memory and disk

Created on 28 Oct 2015  ·  1Comment  ·  Source: onevcat/Kingfisher

Hi all developers
The framework work perfect with Swift for downloading images and store in cache. I set cache.maxDiskCacheSize = 30 * 1024 * 1024 and cache.maxMemoryCost = 30 * 1024 * 1024. Max 30 Mb but it still save cache size of memory and disk over than 30 Mb.
How do I set maximum cache size of memory and disk ?

Most helpful comment

I guess you are doing the right thing. But I just want to notice some detail here.

The memory size will be restricted to the limitation you set. However, instead of the byte size in memory, it is a "cost" related to your image resolution. Currently, Kingfisher is using the pixels count for it.

maxMemoryCost: The largest cache cost of memory cache. The total cost is pixel count of all cached images in memory.

When new data is being stored in memory, the total cost in cache will be checked and compared to your setting. If there is no enough cost for your new image, the largest one already in the cache will be purged. So cache.maxMemoryCost = 30 * 1024 * 1024 means 30M pixels instead of 30M bytes. If you have a 32-bits depth RGBA image (that means white color is FF FF FF FF), 1 pixel would be 4 bytes, so the limit size would be 120MB for you (if you only store 32-bit images).

The disk cache will not be purged until you switch your app to background or you call the cleanExpiredDiskCacheWithCompletionHander method manually. It will swipe the your disk cache to a size under the limitation.

And of course you need to set these properties on the correct cache. By default, Kingfisher is using the ImageCache.defaultCache for the UIImageView extension and default KingfisherManager.

>All comments

I guess you are doing the right thing. But I just want to notice some detail here.

The memory size will be restricted to the limitation you set. However, instead of the byte size in memory, it is a "cost" related to your image resolution. Currently, Kingfisher is using the pixels count for it.

maxMemoryCost: The largest cache cost of memory cache. The total cost is pixel count of all cached images in memory.

When new data is being stored in memory, the total cost in cache will be checked and compared to your setting. If there is no enough cost for your new image, the largest one already in the cache will be purged. So cache.maxMemoryCost = 30 * 1024 * 1024 means 30M pixels instead of 30M bytes. If you have a 32-bits depth RGBA image (that means white color is FF FF FF FF), 1 pixel would be 4 bytes, so the limit size would be 120MB for you (if you only store 32-bit images).

The disk cache will not be purged until you switch your app to background or you call the cleanExpiredDiskCacheWithCompletionHander method manually. It will swipe the your disk cache to a size under the limitation.

And of course you need to set these properties on the correct cache. By default, Kingfisher is using the ImageCache.defaultCache for the UIImageView extension and default KingfisherManager.

Was this page helpful?
0 / 5 - 0 ratings