user172902 user172902 - 2 months ago 14
iOS Question

SDWebImage starts to load too late

In my

cellForRowAtIndexPath
, I am using the following function inside
dequeResuableCell
to update the image in the cell

let imageNSURL = NSURL(string: post.imageUrl)
let placeHolderImage = UIImage(named: "LoadingPlaceHolder")!
postImageView.sd_setImageWithURL(imageNSURL, placeholderImage: placeHolderImage, options: [], progress: { (byteSent: Int, byteExpectedToSend: Int) in
// Progress

}, completed: { (image: UIImage!, error: NSError!, imageCacheType: SDImageCacheType, url: NSURL!) in
if error != nil {
print("Error \(error.localizedDescription)")
} else {
// Image Download Complete
}
})


However, every time the cell is "just about to appear", I see a glimpse of the placeHolder image. It seems like the
SDWebImage
SDK only starts downloading the image close to when the cell actually appears on the screen. I was wondering if anyone else is experiencing this or knows how it could be fixed?

----Update----

In response to the comments

I thought cellForRowAtIndexPath is called way ahead of cell being actually displayed (2,3 cells?). willDisplayCell is the one that is displayed just before the cell is shown. But I guess Im wrong as what you said made sense. I was't scrolling very fast and the file is only 55kb. Caching the image works but that is only after the image gets downloaded. What I want to achieve is that the image gets downloaded maybe 2,3 cells earlier so that it is ready in the cache for the cell to be loaded.

Answer

What you are seeing is probably network latency.

Depending on the cache state of the url you are loading, SDWebImage will probably be downloading the image from a remote server and depending on the size and network type this could take a few moments.

You aren't trying to load the image until the cell is dequeued, this only happens moments before the cell is displayed. You probably want to look at something like SDWebImagePrefetcher. This would allow you to cache the images at an earlier stage in your app lifecycle and should (in most cases) ensure the images are available locally prior to showing the cell eliminating network latency.

Comments