Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDWebImage changing images while tableview scrolling

There is a TableView with Cells and there is ImageView in each of them. I use SDWebImage to load images and it works fine, but when I scroll fast - I see that images in cells are changing few times before it will be right image.

Why is it happening? What should I do?

p.s. I tried use it with or without placeholder with same result

imageView.sd_setImage(with: URL(string : myImageURL), placeholderImage: UIImage(named: "default.jpg"))
like image 971
moonvader Avatar asked Jan 05 '23 13:01

moonvader


2 Answers

In your prepareForReuse() try adding:

imageView.sd_cancelCurrentImageLoad()

It will cancel the previous image downloading and then it won't happen.

Also, I would suggest using the sd_setImage with the completion block, that way you can also make sure it loads correctly.

like image 90
unkgd Avatar answered Jan 19 '23 00:01

unkgd


// This will prevent sdwebimage load wrong url
guard let imgUrl = "Your image url.jpg" else { return }
imageView.sd_setImage(with: URL(string: imgUrl), placeholderImage: UIImage(named: "Your placeholder image.png"))


// Or you can match the url using this code, so the downloader won't catch the wrong url
if imgUrl == "Your image url" {
    imageView.sd_setImage(with: URL(string: imgUrl), placeholderImage: UIImage(named: "Your placeholder image.png"))
}


// This is will clean your UIImageView, and cancel download progress when cell is not visible (will reuse)
class YourCell: UITableViewCell {
    override prepareForReuse() {
        imageView.sd_cancelCurrentImageLoad()
        imageView.image = nil
    }
}
like image 26
Andrian Rahardja Avatar answered Jan 19 '23 01:01

Andrian Rahardja