在UITableView中延迟加载多个图像 [英] Lazy Loading of several images in UITableView

查看:78
本文介绍了在UITableView中延迟加载多个图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下载多个图像并在UITmageView内部的UIImageView中加载每个图像的最佳做法是什么?特别是,我应该在下载后调整大小/替换UIImageview,还是应该调整图像大小以适应UIImageView。请注意,调整大小/替换UIImageView也会调整大小/替换UITableViewCell。它会引起任何问题吗?

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?

推荐答案

一些想法:


下载多个图像并在 UIImageView 中加载每个图像的最佳做法是什么?这些图像位于 UITableViewCell 中?

What are the best practices to download several images and load each of them in a UIImageView which is inside a UITableViewCell?

有关下载图像的最佳做法包括:

The best practices regarding the downloading of images include:


  1. 绝对可以利用延迟加载(在需要时加载图像而不是之前加载图像)。

  1. Definitely avail yourself of lazy loading (load images as you need them and not before).

异步下载图像。

确保您的下载技术将取消对不再可见的tableview单元格的请求。例如,如果您使用的是慢速网络并在桌面视图中快速向下滚动,则不希望系统下载不再可见的图像。

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.

确保您没有为服务器发出太多并发请求。在iOS中,当您超过5或6个并发请求时,后续请求将冻结,直到前一个请求完成。在最坏的情况下,后续请求实际上会在超时时开始失败。

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.

如果你要去为异步操作,缓存等编写自己的代码,您可能希望使用 NSOperationQueue 而不是GCD,这样我就可以约束后台请求数量并发出请求可取消。您可以使用 NSCache 缓存图像。并且您可能使用 UITableViewCell 子类(或类别),以便您可以保存对previous的引用操作,以便您可以取消任何不完整的先前请求。

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.

正如您所看到的,这是非常重要的,我建议您使用现有的 UIImageView 类别,例如 SDWebImage AFNetworking 。恕我直言,前者有点丰富(例如提供磁盘缓存),但如果你做了很多网络并且想要坚持使用单一框架, AFNetworking 会做一个好的工作。

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.

稍后你问:


特别是,我应该下载后调整大小/替换UIImageview,或者我应该调整图像大小以适应UIImageView。请注意,调整大小/替换UIImageView也会调整大小/替换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?




  1. 如果您的图像大于单元格的缩略图视图要求,你有两种方法。首先,您可以使用 contentMode UIView.html#// apple_ref / doc / c_ref / UIViewContentModerel =nofollow noreferrer> UIViewContentModeScaleAspectFit UIViewContentModeScaleAspectFill (如果您使用 AspectFill ,请确保您还将 clipsToBounds 设置为)。更好的是,您下载后可以实际调整图片大小

  1. 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.

这是个人观点,但我认为在单元格上有一个固定大小的 UIImageView 是一个更好的用户体验,然后在异步图像下载完成后,只需设置 UIImageView 图像属性。您希望图像在下载时优雅地显示在UI中,但是当用户已经在阅读那里的内容时,您通常不希望对视图进行重新布局。如果您的设计绝对需要重新布局单元格,那么您只需调用 reloadRowsAtIndexPaths

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.

这篇关于在UITableView中延迟加载多个图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆