Search code examples
iosswiftsdwebimage

SDWebImage Wrong Cell Image


Im using SDWebImage pod for donwload and show images from json and its working well but I have a big problem with it.

Im using collectionView and when cell is loading, some cells showing wrong image. I searched it and I tried so many solutions like image = nil and sd_cancelCurrentImageLoad() in prepareForReuse func on Cell class but not working.

I could not find any solution for solve it, please help guys thanks :)

YazilarCollectionViewCell.swift

import UIKit
import SDWebImage

class YazilarCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var haberGorseli: UIImageView!
    @IBOutlet weak var yazarAvatar: UIImageView!
    @IBOutlet weak var baslik: UILabel!

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

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

}

Only Cell Part in YazilarViewController.swift

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YazilarCollectionViewCell", for: indexPath) as! YazilarCollectionViewCell

        if indexPath.row < basliklar.count{
            cell.baslik.text = basliklar[indexPath.row].html2String
            cell.yazarAvatar.sd_setImage(with: URL(string: catResim[indexPath.row]), placeholderImage: UIImage(named: "faiklogo"))
            cell.haberGorseli.sd_setImage(with: URL(string: resimLink[indexPath.row]), placeholderImage: UIImage(named: "faiklogo"))
            cell.yazarAvatar.layer.cornerRadius = cell.yazarAvatar.frame.height/2
            cell.yazarAvatar.clipsToBounds = true

            cell.layer.shadowColor = UIColor.black.cgColor
            cell.layer.shadowOpacity = 0.25
            cell.layer.shadowOffset = CGSize(width: 0, height: 5)
            cell.layer.shadowRadius = 12
            cell.layer.masksToBounds = false
        }

        return cell
    }

Solution

  • thanks for replies guys, I changed wordpress api for images and its working now without any changes :) Maybe you know wordpress does not give you post image url directly, its giving image id and you must to call second api for get image url so its problem for this. I installed "Better REST API Featured Images" plugin for solve it and its working well.