SDWebImage caching is taking time to load the image from cache in uicollectionview cell

2.3k views Asked by At

I am using SDWebImage library to load images in a collection view .

Library downloads the image from server and caches but when I quit my app and re-run , it takes time to load image from cache. ( placeholder image is displayed for 2-3 secs sometimes even more )

Code to load image using SDWebImage:

cell.imgPost.sd_setImage(with: URL(string: (post.lowQualityImgUrl) ?? ""), placeholderImage: UIImage(named:"greybg") , options: .refreshCached, progress: { (recievedSize, expectedSize) in

        progressIndicatorView.progress = CGFloat(recievedSize)/CGFloat(expectedSize)

        }, completed: { (image, error, cachetype, url) in

            if image != nil{
                DispatchQueue.main.async {
                    progresView.removeFromSuperview()
                    progressIndicatorView.removeFromSuperview()
                    cell.progressIndicatorView?.isHidden = true
                    cell.imgPost.image = image

                }
            }
    })

Here the caching option used is refresh cache.

Question: I don't know why collection view cells can't load the images instantly using sdwebimage . As it says sdwebimage is best library for image caching . Can anyone point out solution /problem ?

EDIT I did left comment in their library , I think it's never a good library. https://github.com/rs/SDWebImage/issues/138

Thanks in advance.

1

There are 1 answers

1
Matt On

From SDWebImage documentation, for refreshCached

 * Even if the image is cached, respect the HTTP response cache control, and refresh the image from remote location if needed.
 * The disk caching will be handled by NSURLCache instead of SDWebImage leading to slight performance degradation.
 * This option helps deal with images changing behind the same request URL, e.g. Facebook graph api profile pics.
 * If a cached image is refreshed, the completion block is called once with the cached image and again with the final image.
 *
 * Use this flag only if you can't make your URLs static with embedded cache busting parameter.

You should not use refreshCached if you don't want images to be cached unless you get different images in same URL. Leave it to default set up and it will handle everything for you.

EDIT: Just leave options as empty array, []. It should continue with default setup. Your delay is probably because you still have refreshedCache in your code.

cell.imgPost.sd_setImage(with: NSURL() as URL!, placeholderImage: UIImage(), options: [], progress: { (_, _) in

}, completed: { (_, _, _, _) in

})