I'm using Kingfisher to download and cache my images. I'm using the custom class below CustomImageView that extends ImageView. I also am using my custom UITableViewCell to call the loadImage method within the didSet property in my CustomCell class file.
The below method loadImage is where all the magic happens.
class CustomImageView : UIImageView {
var lastUrlLoaded: String?
func loadImage(url:String) {
lastUrlLoaded = url
ImageCache.default.retrieveImage(forKey: url, options: nil) { (image, cacheType) in
if let image = image {
self.image = image
return
}
else {
guard let url = URL(string: url) else { return }
self.kf.setImage(with: url, placeholder: nil, options: nil, progressBlock: nil) {
(image, error, cacheTye, _) in
if let err = error {
self.kf.indicatorType = .none
DispatchQueue.main.async {
self.image = #imageLiteral(resourceName: "no_image_available")
}
print ("Error loading Image:", err)
return
}
if url.absoluteString != self.lastUrlLoaded { return }
if let image = image {
ImageCache.default.store(image, forKey: url.absoluteString)
DispatchQueue.main.async {
self.image = image
}
}
}
}
}
}
// Custom TableViewCell -- NewsCell.file
class NewsCell: UITableViewCell {
var article: Article? {
didSet{
guard let image_url = article?.image_url else { return }
_image.kf.indicatorType = .activity
_image.loadImage(url: image_url, type: "news")
}
}
}
// METHOD in my NewsController
override func tableView(_ tableView: UITableView, cellForRowAt indexPath:
IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath) as! NewsCell
cell.article = articles[indexPath.item]
return cell
}
I'm having an issue when scrolling fast through my cells, some images that have loaded are being used for the wrong cells. I've researched and read other post but no answers helped me. I understanding it's dealing with the background task but just not sure how to fix it. If someone can help or point me in the right direction it would be much appreciated.
Solutions I've seen on stackoverflow and tried:
- prepareForReuse in custom cell setting the image to nil.
- setting the image to nil at cellforrowatindexpath.