Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

135
Visualizações
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

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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
    }
}   
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda