Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
2 of 3 tasks
baronha opened this issue Feb 21, 2024 · 2 comments
Open
2 of 3 tasks

Comments

@baronha
Copy link

baronha commented Feb 21, 2024

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)
           }
       }
   }
@onevcat
Copy link
Owner

onevcat commented Feb 22, 2024

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.

@baronha
Copy link
Author

baronha commented Feb 22, 2024

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants