本文介绍了将数据异步加载到表视图单元格中的正确方法/接受标准是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多应用程式,例如,,等显示来自API的图片似乎以异步方式加载它们。看起来初始可视图像被同步加载,也就是说,初始可视单元在它们的图像准备好之前不被显示 - 但是当进一步向下滚动经过初始可视单元时,看起来这些新单元的图像独立于初始同步加载。

Many apps such as Tweetbot, Twitterrific, Alien Blue, etc. that display images from an API seem to load them in an asynchronous manner. It seems the initial viewable images are loaded synchronously – that is the initial viewable cells aren't displayed until their images are ready – but when scrolling further down past the initial viewable cells, it seems the images for those new cells are loaded independently of the initial synchronous load.

例如,当用户在Twitter应用程序中加载tweets时,将显示最初的六个或七个tweets,用户的头像已经加载,但对于之后的40多个tweets(在用户滚动之前不可见),我们不必等待那些图像被加载以显示初始单元格,因为他们甚至不可见。似乎大多数应用程序允许这些图像独立加载,让初始的tweets显示得更快。

For example, when the user loads tweets in a Twitter app, the initial six or seven tweets that will be visible aren't shown until the images for the users' avatars have loaded, but for the 40+ tweets after that (that aren't visible until the user scrolls), we don't have to wait for those images to have loaded in order to display the initial cells, as they're not even visible yet. It seems most apps allow those images to load independently allowing the initial tweets to show up quicker.

我很困惑如何最好地实现这一点,例如,如果有一个标准

I'm confused how to best accomplish this, for example if there's a standard way that most developers go about doing this that I'm not aware of.

我习惯于通过从响应中提供对象到Core Data来使用Core Data,然后 NSFetchedResultsController 在表视图中显示它们。然而,这具有在可以显示单元之前要求一切都被完全加载并存储在对象中的负面影响。这意味着如果API返回一个图像链接到我,我想在单元格中显示该图像,我必须加载链接到 UIImage 并将其添加到Core数据。

I'm accustomed to using Core Data by supplying objects to Core Data from the response, then NSFetchedResultsController displays them within the table view. However this has the negative affect of requiring everything to be fully loaded and stored in objects before the cells can be displayed. This means that if the API returns an image link to me and I want to display that image within the cell, I have to load the link into a UIImage and add it to Core Data.

所以我的问题基本上归结为:什么是流行的应用程序,如Tweetbot,Twitterrific,Alien Blue等处理需要加载大量图片和数据的表视图,但仍然允许非常快速的加载,而不必加载不必要的数据,并仍然保持高滚动性能?

So my question basically boils down to: what do popular apps such as Tweetbot, Twitterrific, Alien Blue, etc. do to handle table views with a lot of images and data needing to be loaded, but still permit very quick loading, without having to load unnecessary data, and still maintaining high scrolling performance?

推荐答案

有很多解决方案,你可以在堆栈溢出找到很多他们。

There are many solutions to this and you can find a lot of them here on stack overflow.

以在块或 NSOperation 中加载图像。我更喜欢 NSOperation ,因为您可以取消它们,这在细胞滚动屏幕时很有用。

The basic idea is you want to load the image either in a block or an NSOperation. I prefer an NSOperation because you can cancel them which is useful when a cell scrolls off screen.

控制器来管理你的镜像队列。你的表单元格从控制器请求图像,如果控制器有图像,它立即返回。如果没有,则返回nil并获取图像。

I recommend a controller to manage your image queue. Your table cell requests the image from the controller and if the controller has the image it returns it immediately. If it doesn't then it returns nil and fetches the image.

从这里有选项。您可以使用块来检索映像时回调。我不是那个的粉丝,但它是相当受欢迎。当接收到图像并且单元侦听 NSNotification 以填充时,我更喜欢使用 NSNotification

From here there are options. You could use a block to call back when the image is retrieved. I am not a fan of that but it is fairly popular. I prefer to use a NSNotification when the image is received and the cell listens for the NSNotification to populate.

我有一个相当复杂的样例在github上处理这个以及更多。它位于我的共享存储库中,位于。主类是 ZSAssetManager

I have a fairly complicated sample up on github that handles this as well as much much more. It is in my shared repository at http://github.com/ZarraStudios/ZDS_Shared. The main class is calledZSAssetManager.

注意:此示例代码是一些岁,所以它不是 ARC 准备好,将采取一些调整。它也可能比你想要的更复杂,因为它处理带宽检测和反应以及。但它会告诉你一个生产/高品质的应用程序处理图像的异步加载。

Note: This sample code is a few years old so it is not ARC ready and will take some tweaking. It is also probably more complicated than you are looking for as it handles bandwidth detection and reaction as well. However it shows you what a production/high quality application does to handle asynchronous loading of images.

享受。

这篇关于将数据异步加载到表视图单元格中的正确方法/接受标准是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 00:42