iosswiftuiimageviewnscachekingfisher

Set UIImage to UIImageView directly with kingfisher


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


Solution

  • 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
        }
    }