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

Using cache key to load image with ui image view #2181

Open
ElrB opened this issue Dec 18, 2023 · 5 comments
Open

Using cache key to load image with ui image view #2181

ElrB opened this issue Dec 18, 2023 · 5 comments

Comments

@ElrB
Copy link

ElrB commented Dec 18, 2023

Hi Guys,

I am using kingfisher in a messaging app, and I would like to know how to load images with ui image view (not to retrieve) using a cache key. So this is what happens:

  1. Current user sends a photo image message, and this image is manually cached immediately using a specific chat message cache and cache key.
  2. The collection view is reloaded to show the message, and right now I use the cache.retrieveImage, then set it in completion block, and as result this causes a few issues, such as image flickering and slight delay in showing image, as images are stored to disk.

I want to load image onto image view using cache key, so that I can make use of .keepCurrentImageWhileLoading, .loadDiskFileSynchronously.

I cannot use url as image is not downloaded. I saw that there is a way to use a LocalFileImageDataProvider, but in this case the image a stored using kingfisher.

Thanks.

@ElrB
Copy link
Author

ElrB commented Dec 18, 2023

My current work around:
Since kingfisher promises to use absolute string from url to check if image is cached, I simply create URL(string: "cache key") then pass it in setImage method as url (that way I get to use setImage options input param). Of course this caused me to force unwrap url, but I first check if image is cached.

@onevcat
Copy link
Owner

onevcat commented Dec 19, 2023

You can create an image resource with a customized cache key and load it:

let resource = KF.ImageResource(downloadURL: url, cacheKey: "my_cache_key")
imageView.kf.setImage(with: resource)

@ElrB
Copy link
Author

ElrB commented Dec 19, 2023

The thing is, the images are not being downloaded in the first place, but manually cache them, then try to set them onto image view through imageView.kf.setImage. As you know setImage does not take cache key, that I used to cache the image. I tried using KF.ImageResource but I don't have a url for the image, and I think the resource here is used to download and cache image under specific key. All in all, just note that I am not downloading the images but trying to use imageView.kf.setImage in order to take advantage of the options param.

@onevcat
Copy link
Owner

onevcat commented Dec 19, 2023

If there is already a place to load the image (and you will always have it locally), then maybe instead of the ImageResource (which is basically for remote image), you can use one of the ImageDataProvider to provide the image. Then, Kingfisher will handle the left.

Check this for detail: https://github.com/onevcat/Kingfisher/wiki/Cheat-Sheet#imagedataprovider

Or as long as you can get the raw data of the image somewhere, you can also create your own data provider.


If you only have the local image transiently, and in the future there might be a chance you need to load it from a remote URL, then you can firstly manually cache it in the ImageCache with a cache key, then create an ImageResource to load it, from cache first, and if not found, download from the remote URL.

@ElrB
Copy link
Author

ElrB commented Dec 19, 2023

Is there an ImageDataProvider to load an image from kingfisher cache? If yes, kindly comment. I only know of LocalFileImageDataProvider, which loads from app files.

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