SDWebImage + UITableViewCell, wrong image when scrolling?

前端 未结 5 1908
失恋的感觉
失恋的感觉 2021-02-06 04:08

i\'m having an issue using SDWebImage to load images to a UIImageView inside a custom UITableViewCell. This is my code at the UITableView delegate:

    static NS         


        
5条回答
  •  长情又很酷
    2021-02-06 04:48

    I was getting blank images with the accepted answer and given the images I'm loading are small, I wanted to let the images cache in the background and not cancel the load.

    1. Push a unique id on the stack before your closure and check it when your closure completes
    2. prepareForReuse

    Like this:

    func updateArtistImage(url: URL) {
            let _eventId = self.event?.id
            SDWebImageManager.shared().loadImage(with: url, options: [], progress: nil) { (image, data, error, cacheType, finished, url) in
                if self.event!.id == _eventId {
                    if error == nil {
                        self.artistImageView.image = image
                    } else {
                        self.artistImageView.image = UIImage(named: "error_image")
                    }
                }
            }
        }
    

    and this:

    override func prepareForReuse() {
        super.prepareForReuse()
        self.artistImageView.image = nil
    }
    

提交回复
热议问题