我正在使用 af_setImageWithURL
Alamofire API 来获取表格单元格图像并传递默认占位符图像:
let defaultImage = UIImage(named:"myImage")
cell.imageView.af_setImageWithURL(imageURL, placeholderImage:defaultImage, imageTransition: .CrossDissolve(0.1))
问题: 当下载路径中不存在我的图像时,单元格也不会显示默认图像。当我向上和向下滚动表格(清除并重新使用同一个单元格)时,它就会出现。
编辑:
注意到这是在单元格出队后发生的,我先重置图像,然后调用 Alamofire。如果我在下面注释掉,它工作正常,但如果图像 URL 和默认图像都不可用,我就会遇到显示旧产品图像的风险。
cell.imageView.image = nil
有哪位遇到过类似问题,请指教。
最佳答案
您需要确保您取消了您的请求,并且 nil
了 prepareForReuse
中的图像。否则你会遇到一些不同的问题。这是 AFI 中 iOS 示例
中的 ImageCell
类。
class ImageCell: UICollectionViewCell {
class var ReuseIdentifier: String { return "org.alamofire.identifier.\(type(of: self))" }
let imageView: UIImageView
// MARK: - Initialization
override init(frame: CGRect) {
imageView = {
let imageView = UIImageView(frame: frame)
imageView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
imageView.contentMode = .center
imageView.clipsToBounds = true
return imageView
}()
super.init(frame: frame)
contentView.addSubview(imageView)
imageView.frame = contentView.bounds
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// MARK: - Lifecycle Methods
func configureCell(with URLString: String, placeholderImage: UIImage) {
let size = imageView.frame.size
imageView.af_setImage(
withURL: URL(string: URLString)!,
placeholderImage: placeholderImage,
filter: AspectScaledToFillSizeWithRoundedCornersFilter(size: size, radius: 20.0),
imageTransition: .crossDissolve(0.2)
)
}
override func prepareForReuse() {
super.prepareForReuse()
imageView.af_cancelImageRequest()
imageView.layer.removeAllAnimations()
imageView.image = nil
}
}
干杯。 🍻
关于ios - Alamofire 图片 : Placeholder image is not set for af_setImageWithURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39097877/