How to load image with SDWebImage correctly in UIC

2019-08-19 08:31发布

问题:

I'm facing the following problem, well I have a custom collection view, where I upload an image from the URL I get from a service, everything works fine at the moment but sometimes when scrolling or when I leave the screen and return, my collection view customized it is altered, it does not load correctly, I have seen several posts where they present the same problem in different cases, but trying to conclude that my problem is at the moment of using the SDWebImage library. I have seen that they use methods like the prepareForReuse setting the image to nil but still my problem persists.

So you see the first time

and then it looks like this the second time

Here this my code

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "ItemMenuFilterCollectionViewCell", for: indexPath) as! ItemMenuFilterCollectionViewCell
         cell.mImgProduct.image = nil
         cell.mImgProduct.sd_setImage(with: URL(string: mDataListProducts[indexPath.row].photo!), placeholderImage: UIImage(named: "placeholder.png"))

        cell.mTxtProductName.text = mDataListProducts[indexPath.row].shortName
        cell.mTxtBrand.text = mDataListProducts[indexPath.row].marca
        cell.mTxtStore.text = mDataListProducts[indexPath.row].tienda
        cell.mTxtPrice.text = mDataListProducts[indexPath.row].price
        cell.mRatingStar.rating = mDataListProducts[indexPath.row].valor?.toDouble() ?? 0

        return cell

    }

As I mentioned, I think that my problem is in charge of the image with the libreta since I comment on that line of code cell.mImgProduct.sd_setImage(with: URL(string: mDataListProducts[indexPath.row].photo!), placeholderImage: UIImage(named: "placeholder.png")) All function ok, I would like to know a better way to do it or maybe correct something that is happening to me.

I also have this in the prepareforReuse of my customviewcell but still nothing.

class ItemMenuFilterCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var mRatingStar: CosmosView!
    @IBOutlet weak var mImgProduct: UIImageView!
    @IBOutlet weak var mTxtProductName: UILabel!
    @IBOutlet weak var mTxtBrand: UILabel!
    @IBOutlet weak var mTxtStore: UILabel!
    @IBOutlet weak var mTxtPrice: UILabel!

    override func prepareForReuse() {
        self.mImgProduct.sd_cancelCurrentImageLoad()
        self.mImgProduct.image = nil
    }
}