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 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?
相关问题
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Swift - hide pickerView after value selected
- Popover segue to static cell UITableView causes co
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
A common practice for lazy-loading images in a
UITableViewCell
is to use a notification callback to let theUITableViewCell
know when the image has been received.In essence, you'll want to create a subclass of
UIImageView
that has an imageURL field that, when changed, fires off a request for the image, and use that instead of a standardUIImageView
:Interface for the UIImageView subclass:
Implementation for the UIImageView subclass:
And then in the UITableViewCell class when you override prepareForReuse:
Now, as far as the whole re-sizing of the images vs resizing of the imageview, what you ought to do is leave the imageView's size alone, and use the
contentMode
property to handle the varying sizes for the resulting images. Your possible values are:Each of which has their own respective result - you will probably want to use
UIViewContentModeScaleAspectFit
as this will re-size the image to fit the imageView, without distorting it - this may leave empty margins to the size or top/bottom of the imageView. Aspect fill will do a similar thing, only it will resize the image to be large enough to fill the entire imageView, and may cut off the sides or top/bottom of the image. Scale to fill will just stretch the image to fill the imageView.A couple of thoughts:
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 useNSCache
to cache the images. And you'd probably use aUITableViewCell
subclass (or a category) so that you can saveweak
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 ofSDWebImage
orAFNetworking
. 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:
If your images are larger than what your cell's thumbnail view requires, you have two approaches. First, you can use a
contentMode
ofUIViewContentModeScaleAspectFit
orUIViewContentModeScaleAspectFill
(and if you useAspectFill
, make sure you also setclipsToBounds
toYES
). 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 theimage
property of theUIImageView
. 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 callreloadRowsAtIndexPaths
.