UITableView 中多张图片的延迟加载 [英] Lazy Loading of several images in UITableView

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

问题描述

下载多个图像并将每个图像加载到 UITableViewCell 内的 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?

推荐答案

几个想法:

下载多个图像并将每个图像加载到 UITableViewCell 内的 UIImageView 的最佳做法是什么?

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

异步下载图片.

确保您的下载技术将取消对不再可见的表格视图单元格的请求.例如,如果您的网络速度较慢,并且在表格视图中快速向下滚动,您不想占用您的设备下载不再可见的图像.

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 子类(或类别),以便您可以保存 weak 对先前"操作的引用,以便您可以取消任何不完整的、先前的请求.

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 类别,例如作为 SDWebImageAFNetworking.恕我直言,前者更丰富一些(例如提供磁盘缓存),但如果您正在做大量网络工作并希望坚持使用单一框架,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. 如果您的图像大于单元格缩略图视图所需的大小,您有两种方法.首先,您可以使用 UIViewContentModeScaleAspectFitUIViewContentModeScaleAspectFill(如果您使用 AspectFill,确保您还将 clipsToBounds 设置为 YES).更好的是,您实际上可以在下载后调整图像大小.

  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 的 image 属性.您希望图像在下载时优雅地出现在您的 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天全站免登陆