Lazy Loading of several images in UITableView(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 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).
异步下载图片.
确保您的下载技术将取消对不再可见的表格视图单元格的请求.例如,如果您的网络速度较慢,并且在表格视图中快速向下滚动,您不想占用您的设备下载不再可见的图像.
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
类别,例如作为 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?
如果您的图像大于单元格缩略图视图所需的大小,您有两种方法.首先,您可以使用 contentModeapple_ref/doc/c_ref/UIViewContentMode" rel="nofollow noreferrer">
UIViewContentModeScaleAspectFit
或UIViewContentModeScaleAspectFill
(如果您使用AspectFill
,确保您还将clipsToBounds
设置为YES
).更好的是,您实际上可以在下载后调整图像大小.
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.
这是个人意见,但我认为在单元格上有一个固定大小的 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 中多张图片的延迟加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UITableView 中多张图片的延迟加载
基础教程推荐
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01