I am using af_setImageWithURL
Alamofire API to fetch the table cell images and pass a default placeholder image:
我正在使用af_setImageWithURL Alamofire API来获取表格单元格图像并传递默认的占位符图像:
let defaultImage = UIImage(named:"myImage")
cell.imageView.af_setImageWithURL(imageURL, placeholderImage:defaultImage, imageTransition: .CrossDissolve(0.1))
Problem: When my image does not exists at the downloading path, cell do not show the default image as well. When I scroll the table up & down (to purge and re-use the same cell) then it shows up.
问题:当下载路径中不存在我的图像时,单元格也不显示默认图像。当我向上和向下滚动表格(清除并重新使用同一个单元格)时,它会显示出来。
EDIT:
Noticed this is happening after dequeuing the cell, I reset the image first and then call Alamofire. If I comment out below line, it works fine but then I am running into a risk of showing old product image if both image URL and default images are not available.
注意到这是在将单元格出列后发生的,我首先重置图像然后调用Alamofire。如果我在下面注释掉它,它运行正常,但如果图像URL和默认图像都不可用,那么我将面临显示旧产品图像的风险。
cell.imageView.image = nil
Anybody faced similar issue, please advise.
任何人都面临类似的问题,请指教。
2 个解决方案
#1
1
You need to make sure you cancel your request and also nil
out the image in prepareForReuse
. Otherwise you can run into a few different problems. Here's the ImageCell
class in the iOS Example
in AFI.
您需要确保取消您的请求,并在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
}
}
Cheers. ????
#2
0
As I mentioned, this works well if I do not set cell image view' image nil just before Alamofire call. So, I am now setting the cell image view' image to nil only when default image is nil.
正如我所提到的,如果我在Alamofire调用之前没有设置单元格图像视图'image nil,这很有效。所以,我现在只在默认图像为零时将单元格图像视图'图像设置为nil。
cell.imageView.image = nil
#1
1
You need to make sure you cancel your request and also nil
out the image in prepareForReuse
. Otherwise you can run into a few different problems. Here's the ImageCell
class in the iOS Example
in AFI.
您需要确保取消您的请求,并在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
}
}
Cheers. ????
#2
0
As I mentioned, this works well if I do not set cell image view' image nil just before Alamofire call. So, I am now setting the cell image view' image to nil only when default image is nil.
正如我所提到的,如果我在Alamofire调用之前没有设置单元格图像视图'image nil,这很有效。所以,我现在只在默认图像为零时将单元格图像视图'图像设置为nil。
cell.imageView.image = nil