How to load image with SDWebImage correctly in UICollectionView in swift

放肆的年华 提交于 2019-12-11 16:39:32

问题


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

来源:https://stackoverflow.com/questions/56571371/how-to-load-image-with-sdwebimage-correctly-in-uicollectionview-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!