Kingfisher icon indicating copy to clipboard operation
Kingfisher copied to clipboard

Add `.data` property to `RetrieveImageResult`

Open sindresorhus opened this issue 6 years ago • 8 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 use Kingfisher to show a bunch of animated GIFs in a macOS app. For drag and drop, I want to use a higher resolution image than what's shown in the app. So my plan is to prefetch the originals in the background using ImagePrefetcher after the previews have downloaded, and then use KingfisherManager.shared.retrieveImage to get the originals when the user drags. The original GIF has to be on disk for drag and drop (only way to drag and drop animated GIFs), so I need to write the fetched image to a temporary directory. The problem is that RetrieveImageResult only provides a image property with a NSImage instance. I cannot easily write an NSImage to disk as an animated GIF. So I was hoping you would be willing to add a .data property which would contain the raw bytes of the image (Data type).

sindresorhus avatar Mar 14 '20 18:03 sindresorhus

RetrieveImageResult is not only representing an image from the network, instead, it is a general type for all image retrieving, including from disk or memory. In some cases, such as getting images from the memory cache, it does not make much sense to provide the data.

For your task, I guess instead of exposing a data property, a better approach would be implementing your own CacheSerializer. There you can get the original data let Kingfisher to just write the original data (you can also hook it to write it to where you need as side-effect). Maybe something like this:

public struct MyCacheSerializer: CacheSerializer {
  //...
  public func data(with image: KFCrossPlatformImage, original: Data?) -> Data? {
    // Write the `original` data to the path you want.
    return original
  }
}

Then, you can access it directly in the drag and drop logic from that path.

onevcat avatar Apr 11 '20 02:04 onevcat

In some cases, such as getting images from the memory cache, it does not make much sense to provide the data.

Why not?

sindresorhus avatar Apr 14 '20 11:04 sindresorhus

I also need to get Data to store in CoreData

L1cardo avatar May 14 '20 06:05 L1cardo

An accessor for the data here would be very useful to me as well

wilg avatar Mar 11 '21 05:03 wilg

+1

plkgq avatar Jul 13 '22 09:07 plkgq

I need the data, thank you. Now I have to get local data by Data.init and get network data by using ImageDownloader. It's a pity that cache means nothing in this situation.

wjling avatar Aug 05 '22 03:08 wjling

It is not free to contain a stored property for the data. For most use cases, the data is not necessay.

Adding a function to get (or say, create) the data is possible. I will see a way to implement it soon.

onevcat avatar Aug 05 '22 03:08 onevcat

Can we add a option that cache original data as well. Maybe a new KingfisherOptionsInfoItem or something. Then like images, we can get the original data from memory, disk or network without processing it to UIImage

wjling avatar Aug 05 '22 06:08 wjling