How to cache images only in disk using Kingfisher?

后端 未结 3 1315
梦毁少年i
梦毁少年i 2021-02-20 17:00

I am using Kingfisher library for downloading and caching images. I am facing some issues in the implementation:

  1. Are the images ca

3条回答
  •  北海茫月
    2021-02-20 17:37

    Yes, Kingfisher caches images both in memory and on disk.

    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
    

    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).

    For example, in my tests, the maximum RAM used with a value of 1024 * 1024 * 500 fluctuates between 120MB and 300MB.

    Incidentally, this is also how you tell Kingfisher to never use the RAM and only cache to disk:

    ImageCache.default.maxMemoryCost = 1
    

    This will force Kingfisher to only use the disk cache.


    How to debug

    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.

    You may also want to manually clear the memory cache and compare the RAM occupation before and after:

    ImageCache.default.clearMemoryCache()
    

    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)
    }
    

提交回复
热议问题