How to Cache Images Only in Disk Using Kingfisher

How to cache images only in disk using Kingfisher?

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

Kingfisher cache image to disk

The downloader (ImageDownloader) has nothing to do with cache. If you want to download and cache (as well as retrieve the cached images later), you should use the manager method: KingfisherManager.shared.retrieveImage instead of downloader standalone.

What's the Kingfisher default cache behavior?

Kingfisher is using the whole url as the cache key by default, so you have to ensure that you have the very SAME url string for the images every time you request your API. If there is something like timestamp or version number appended to the urls, the cache will fail. (eg. http://example.com/image.png?time=123 and http://example.com/image.png?time=456 will be recognized as two different cache keys, although you could get the same image from it).

This is the only case I could draw for your operation. If this is true for you, you could create an ImageResource to specify the cache key explicitly (which should be related to your original url, but get rid of the appended things):

let r = ImageResource(downloadURL: yourUrl, cacheKey: key)
imageView.kf.setImage(with: r, placeholder: yourImage)

How to clear memory and disk cache for images loaded using Kingfisherin UITableView?

On receiving memory warning I cleared the cache and it worked fine for me now :

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
cache.clearMemoryCache()
cache.clearDiskCache()
cache.cleanExpiredDiskCache()
}

Update for Swift 4 :

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

Show updated images in the same url using Kingfisher ios

Kingfisher does not support server cache mechanism. It just uses the whole URL as the local cache key. As long as you use the same URL, you will get the same image from the cache (if it was cached).

So, if your server provides different images under the same URL, we suggest you to ask your server to add a query to the URL for different versions. These URLs: "https://example.com/image.png?v=1" and "https://example.com/image.png?v=2" represent different images in Kingfisher and it could work well. At the same time, access to the "https://example.com/image.png?v=2" would navigate you to the "https://example.com/image.png" data for most implementation of the server.

You can find more information on this page:

https://github.com/onevcat/Kingfisher/wiki/FAQ#does-kingfisher-support-server-cache-tags-like-e-tag-or-last-modified

Swift App Termination removes Kingfisher Disk Cache

yes, this is possible. you can give a directory url like this: ImageCache(name: "test", cacheDirectoryURL: url) -> url could be your documents folder e.g.



Related Topics



Leave a reply



Submit