簡體   English   中英

SDWebImage錯誤的單元格圖像

[英]SDWebImage Wrong Cell Image

我使用SDWebImage pod進行下載並顯示來自json的圖像及其運行良好,但是我遇到了一個大問題。

我正在使用collectionView並且在加載單元格時,某些單元格顯示錯誤圖像。 我搜索了它,並在Cell類的prepareForReuse函數中嘗試了很多解決方案,例如image = nilsd_cancelCurrentImageLoad() ,但不起作用。

我找不到解決方案,請幫忙謝謝:)

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

}

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
    }

嘗試這個

func collectionView(_ collectionView:UICollectionView,cellForItemAt indexPath:IndexPath)-> UICollectionViewCell {讓單元格= collectionView.dequeueReusableCell(withReuseIdentifier:“ YazilarCollectionViewCell”,for:indexPath)為! YazilarCollectionViewCell

    if indexPath.row < basliklar.count{
        cell.baslik.text = basliklar[indexPath.row].html2String
        cell.yazarAvatar.image = UIImage()
        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
}

感謝您的答復,我更改了wordpress api的圖像,並且現在可以正常使用了,沒有任何改變:)也許您知道wordpress不會直接給您發布圖像url,它給了圖像id,您必須調用第二個api來獲取圖像url,所以它問題。 我安裝了“更好的REST API Featured Images”插件來解決它,並且效果很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM