What are the best practices to download several images and load each of them in a UIImageView which is inside a UITableViewCell? Particularly, should I resize/replace the UI
A couple of thoughts:
What are the best practices to download several images and load each of them in a
UIImageView
which is inside aUITableViewCell
?
The best practices regarding the downloading of images include:
Definitely avail yourself of lazy loading (load images as you need them and not before).
Download images asynchronously.
Make sure your download technique will cancel requests for tableview cells that are no longer visible. For example, if you're on a slow network and scroll down quickly on your table view, you don't want to tie up your device downloading images that aren't visible anymore.
Make sure you don't make too many concurrent requests of your server. In iOS, when you exceed 5 or 6 concurrent requests, subsequent requests will freeze until the prior ones complete. In worst case scenarios, the subsequent requests will actually start failing as they timeout.
Cache your results. At the very least, cache them in memory. You might also want to cache them to persistent storage (a.k.a. "disk"), too.
If you were going to write your own code for the asynchronous operations, caching, etc. you might want to use NSOperationQueue instead of GCD so that I could constrain number of background requests and make the requests cancelable. You would use NSCache to cache the images. And you'd probably use a UITableViewCell
subclass (or a category) so that you can save weak
reference to "previous" operation, so that you can cancel any incomplete, prior requests.
As you can see, this is non-trivial, and I'd suggest you using an existing UIImageView
category, such as those available as part of SDWebImage or AFNetworking. IMHO, the former is a little richer (e.g. offers disk caching), but if you're doing a lot of networking and want to stick with a single framework, AFNetworking
does a great job, too.
Later you ask:
Particularly, should I resize/replace the UIImageview after downloading or should I resize the image to fit into the UIImageView. Note that resizing/replacing UIImageView also resize/replace UITableViewCell. Will it cause any issue?
If your images are larger than what your cell's thumbnail view requires, you have two approaches. First, you can use a contentMode
of UIViewContentModeScaleAspectFit or UIViewContentModeScaleAspectFill (and if you use AspectFill
, make sure you also set clipsToBounds
to YES
). Even better, you can actually resize the image after you download it.
It's personal opinion, but I think it's a better UX to have a UIImageView
of fixed size on the cell and then when the asynchronous image download is done, just set the image
property of the UIImageView
. You want the images to gracefully appear in your UI as they're downloaded, but you generally don't want a jarring re-layout of the view while the user is already in the process of reading what's there. If your design absolutely necessitates the re-layout of the cell, then you can just call reloadRowsAtIndexPaths.