SDWebImage 错误的细胞图像
SDWebImage Wrong Cell Image
我正在使用 SDWebImage
pod 下载和显示来自 json 的图像,它运行良好,但我有一个大问题。
我正在使用 collectionView
并且在加载单元格时,一些单元格显示错误的图像。我搜索了它,并在 Cell class 上的 prepareForReuse 函数中尝试了很多解决方案,例如 image = nil
和 sd_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 {
让 cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YazilarCollectionViewCell", for: indexPath) as! 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 不会直接给你 post 图像 url,它提供图像 ID,您必须调用第二个 api 来获取图像 url,所以这是问题所在。我安装了 "Better REST API Featured Images" 插件来解决它并且它运行良好。
我正在使用 SDWebImage
pod 下载和显示来自 json 的图像,它运行良好,但我有一个大问题。
我正在使用 collectionView
并且在加载单元格时,一些单元格显示错误的图像。我搜索了它,并在 Cell class 上的 prepareForReuse 函数中尝试了很多解决方案,例如 image = nil
和 sd_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 { 让 cell = collectionView.dequeueReusableCell(withReuseIdentifier: "YazilarCollectionViewCell", for: indexPath) as! 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 不会直接给你 post 图像 url,它提供图像 ID,您必须调用第二个 api 来获取图像 url,所以这是问题所在。我安装了 "Better REST API Featured Images" 插件来解决它并且它运行良好。