SDWebImage, which is widely used as an image cache library, I think that there are many scenes that mainly use the following methods as methods when clearing the cache.
SDImageCache.sharedImageCache().clearMemory()
SDImageCache.sharedImageCache().clearDisk()
But if you want to clear the cache for a particular UIImageView, It is expensive to clear all the caches one by one, so I tried to find out how to clear the cache of a specific UIImageView.
This can be achieved by using the following method.
SDImageCache.shared.removeImageFromDisk(forKey: String)
SDImageCache.shared.removeImageFromDisk(forKey: String)
The argument forKey
(cacheKey) seems to be the URL set in the UIImageView.
▼ Official Document
The cache key is an application unique identifier for the image to cache. It is generally the absolute URL of the image.
I'm a little worried about generally
, but lol (probably because you can specify the key when caching?)
I was able to clear the cache by implementing as follows.
if let cacheKey = uiimageView.sd_imageURL?.absoluteString {
SDImageCache.shared.removeImageFromDisk(forKey: cacheKey)
SDImageCache.shared.removeImageFromDisk(forKey: cacheKey)
}
Recommended Posts