onevcat/Kingfisher

How to refresh cache same url while in use props: .resizing() and downsampling() on SwiftUI

baronha opened this issue · 3 comments

Check List

Thanks for considering to open an issue. Before you submit your issue, please confirm these boxes are checked.

Issue Description

What

I'm making a photo editing app in SwiftUI and I prioritize image display using Kingfisher (perfect library ✨).
After each image creation, I will use Kingfisher to save it to the device's cache (I don't know if this is safe for data or not because I'm just a novice with Swift and Kingfisher).
I don't use remote URLs but use manually generated cacheKey with UUID.

But when used with props: .resizing() and downsampling() - The images are not updated to the latest. Without using the above props, everything works great.

Reproduce

  • View:
let url = recent.getImageURL() // gen by uuid
KFImage(url)
   .resizing(referenceSize: .init(width: size, height: size), mode: .aspectFill)
   .downsampling(size: .init(width: size, height: size))
   // 
   .scaleFactor(UIScreen.main.scale)
   .cacheOriginalImage()
   .waitForCache()
   .onlyFromCache()
   .resizable()
   .scaledToFill()
   .aspectRatio(contentMode: .fill)
   .frame(maxWidth: .half_vw)
   .clipped()

  • Remove Cache:
    func setImage(_ uiImage: UIImage, _ data: Data?) {
       imageSize = uiImage.size // Set image size for recent.

       if let cacheKey = getImageURL()?.cacheKey {
           let cache = ImageCache.default

           DispatchQueue.main.async {
               if cache.isCached(forKey: cacheKey) == true {
                   cache.removeImage(forKey: cacheKey)
               }
               cache.store(uiImage,
                           original: data,
                           forKey: cacheKey)
           }
       }
   }

I am not quite sure about your use case and image updating mechanism, to me, it is not quite a perfect case to use Kingfisher. If you just need to refresh the image every time, you can use the forceRefresh to ask Kingfisher loads from your url instead of seeking it from the cache.

Behind the story, when you are using KFImage(url) to display an image, it in fact tries to download it from the url and caches it with the combination of the cacheKey (by default, the URL string) and the processors identifier. That is because we just want to store the final result instead of the medium artifacts. However, in your code (setImage(_:_:), you are trying to check and cache the image again with the cacheKey alone. With any processors (here resizing and downsampling), the actually used cache key in this method does not equal to the one used by KFImage(url). That means, you are indeed clearing the image and caching it again, but under a different key that KFImage(url).resizing. downsampling is not using.

After each image creation, I will use Kingfisher to save it to the device's cache (I don't know if this is safe for data or not because I'm just a novice with Swift and Kingfisher).

If the images are important, I have to say it is not a good idea. These content are user created and cannot be restored. As the ImageCache's name suggests, it is possible that these cached images being purged (due to system behavior, disk space not enough, normally expires, etc). That is also a reason I said "it is not quite a perfect case to use Kingfisher" at the beginning.

So in my case, would it be reasonable to save the image in FileManager and use KingFisher to display the image via the local url?
Based on your experience, please give me a solution. Please!

@onevcat Can you solve this problem?