1

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.

user3804063
  • 809
  • 1
  • 14
  • 32
  • Why are you setting the image again by using `cell.imgPost.image = image` inside the block.? Its already being set by this `cell.imgPost.sd_setImage(with: ` block. – Poles Dec 30 '16 at 09:28
  • @Poles if i don't place the cell.imgpost.image = image then on scrolling image view shows placeholder again I don't know what i'm missing – user3804063 Dec 30 '16 at 10:02

1 Answers1

0

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

})
Matt
  • 1,711
  • 18
  • 20
  • problem is image is cached properly . but on relaunch it is taking time to load from cache (2-3 secs ) delay why taking this much delay – user3804063 Dec 30 '16 at 10:23