Set UIImage to UIImageView directly with kingfisher
Asked Answered
D

1

6

I want to setImage with UIImage directly and not from any Source, Resource. I already have my image and want to set it with caching to my imageView.

let myImage = UIImage

kingfisherView.kf.setImage(with: myImage)

i want this to be done like i set image to UIImageView i.e

UIImageView.image = myImage

But with caching

i am not downloading image from a source i am generating them by myself (Latex). and caching them with

let cache = ImageCache.default

cache.store(renderedLaTeX ?? UIImage(), forKey: "image\(indexPath.row)")

I just want to set that cached image to my imageView.

UIImage.image = cachedImage

is not working and is loading image again and again as i scroll up and down in CollectionViewCell

Or any other way of doing this so that i don't have load imageView with a image again and again. My ImageView is inside UICollectionViewCell

Droplet answered 21/3, 2019 at 8:41 Comment(2)
I think, hope and believe, that KingFisher use a cache, so if it has already downloaded the image it won't download it again but use the cache version. Maybe a setting to use.Senghor
@Senghor yes the image is stored in cache but problem i am facing is there is no option in kingfisher(or i didn't find one) by which i can directly assign that retrieved image from cache to UIImageView with kingfisherDroplet
I
5

You can store already existing image in Kingfisher cache by:

let image: UIImage = //...
ImageCache.default.store(image, forKey: cacheKey)

By default Kingfisher uses url.absoluteString as cacheKey.

So if you already downloaded image from somewhere and still have this url, you can store them in cache by yourself and next time Kingfisher will not download image, but use cached one

If you just want caching without downloading, then you could retrieve image by:

cache.retrieveImage(forKey: "cacheKey") { result in
    switch result {
    case .success(let value):
        print(value.cacheType)

        // If the `cacheType is `.none`, `image` will be `nil`.
        print(value.image)

    case .failure(let error):
        print(error)
    }
}

But since you using it inside collection view make sure that you stop loading on reuse of collectionViewCell

Example in cell:
We store imageKey inside cell and when Cache return image to us we make sure that cell haven't been reused yet and still need this image. If cell reused, then in prepareToReuse() we delete stored imageKey

class LatexCell: UICollectionViewCell {
    @IBOutlet var formulaImageView: UIImageView!
    private var imageKey: String?

    func setup(with imageKey: String) {
        self.imageKey = imageKey
        ImageCache.default.retrieveImage(forKey: imageKey) { [weak self] result in
            guard self?.imageKey == imageKey else { return } // cell have been reused
            switch result {
            case .success(let value):
                self?.formulaImageView.image = value.image

            case .failure(let error):
                break /// no image stored, you should create new one
            }
        }
    }

    override func prepareForReuse() {
        super.prepareForReuse()
        imageKey = nil
        formulaImageView.image = nil // Probably want here placeholder image
    }
}   
Indication answered 21/3, 2019 at 9:16 Comment(4)
i am not downloading image from a source i am generating them by myself (Latex). and caching them with self.cache.store(renderedLaTeX ?? UIImage(), forKey: "image\(indexPath.row)") I just want to set that cached image to my imageView. UIImage.image = cachedImage is not working and is loading image again and again as i scroll up and won in CollectionViewCellDroplet
@DeepakVerma check updated answer. Hope it helps :)Indication
yes i am doing exactly like this. I just want to display this value.image in my UIImageView. I tried doing this by UIImageView.image = value.image it always reloads because of reusableCells. and as i am displaying large amount of images (around 25) first image appears at the place of 4th. This is the main issue i am facing. Hope i'm able to explain the real issue cz i'm really bad at explaining :pDroplet
@DeepakVerma I add example of handling reuse of cell. You need to make sure that loaded image is still image cell need to displayIndication

© 2022 - 2024 — McMap. All rights reserved.