Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Kingfisher, images are not loading correctly in UITableView

I just started using KingFisher.

When you scroll too fast down a UITableView, all network/doadload request get backed up, so by time it stops scrolling, it's still finishing those previous request.

This leads to all the images flickering around with different pictures until all backed up requests are complete.

Here is my code to retrieve images from cache or network.

if ImageCache.defaultCache.cachedImageExistsforURL(finished_URL!) == true {
            print("image is already cached.")
            
            if cell.tag == indexPath.row{
                cell.card_imageIV.image = UIImage(imageLiteral: "ic_error")
                KingfisherManager.sharedManager.retrieveImageWithURL(finished_URL!, optionsInfo: nil, progressBlock: nil, completionHandler: { (image, error, cacheType, imageURL) -> () in
                    cell.card_imageIV.image = image
                    print("Retrieved cached image")
                })
            }
            
        }else{
            cell.card_imageIV.kf_setImageWithURL(finished_URL!, placeholderImage: self.placeholderImage, optionsInfo: nil, completionHandler: { image, error, cacheType, imageURL in
                print("\(card_name): Finished")
            })
        }

I've tried following documentation on https://github.com/onevcat/Kingfisher for cancelling all previous download tasks, using but didn't help.

Following code I tried:

// The image retrieving will stop.
imageView.kf.cancelDownloadTask()

This only cancels the current cells, at least from I can make sense of.

like image 419
Matthew White Avatar asked May 15 '16 16:05

Matthew White


1 Answers

I have absolutely same issue with my project. I am looking for a way to implement just the same thing as you want: if I scroll VERY fast, – I want to cancel previous downloads. Here is my approach for this, using KingFisher library(Swift 3):

func tableView(_ tableView: UITableView, didEndDisplaying cell: UITableViewCell, forRowAt indexPath: IndexPath) {
    let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentify) 
    cell?.imageView?.kf.cancelDownloadTask()

}

This method must be declared in table view delegate. It will cancel download task task of already scrolled cell.

Hope this helps!

like image 175
An Se Avatar answered Oct 15 '22 06:10

An Se