在TableViewCell中从URL预加载图像

时间:2022-06-23 09:14:07

I'm making a simple rss reader for the iPhone. The feed (http://feeds.feedburner.com/foksuk/gLTI) contains a reference to images that I want to show in my TableView. By default, as far as I understand, the iPhone only tries to load images when they are needed: it will only load the images for the rows that are displayed. Only when the user scrolls down, it will start loading the ones that need to be displayed. Right? Now, since the images are downloaded from the web (they are not too large, but still), the scrolling of my TableView is stuttering. This is in the emulator, so on the device itself it will only be worse. Luckily, this specific feed only lists the latest 12 entries of the blog. So I guess I should be able to first download an cache all the images, so they can be loaded from the memory, rather than from the URL. What is the proper approach here?

我正在为iPhone制作一个简单的rss阅读器。 Feed(http://feeds.feedburner.com/foksuk/gLTI)包含对我想要在TableView中显示的图像的引用。默认情况下,据我所知,iPhone只在需要时尝试加载图像:它只会加载显示的行的图像。只有当用户向下滚动时,它才会开始加载需要显示的那些。对?现在,由于图像是从网上下载的(它们不是太大,但仍然是),我的TableView的滚动就是口吃。这是在模拟器中,因此在设备本身上它只会更糟。幸运的是,此特定订阅源仅列出了博客的最新12个条目。所以我想我应该能够先下载所有图像的缓存,这样就可以从内存中加载它们,而不是从URL加载。这里适当的方法是什么?

1 个解决方案

#1


I had the same problem, where my TableView would have to download each image before it displayed the cell. Like you say, it causes a big slowdown in the scrolling speed. What you need to do is download the images in the background and then insert them into the cell when they're finished downloading. This is how the app store app does it.

我有同样的问题,我的TableView必须在显示单元格之前下载每个图像。就像你说的那样,它会导致滚动速度大幅下降。您需要做的是在后台下载图像,然后在下载完成后将它们插入到单元格中。这就是应用程序商店应用程序的功能。

This post has all you need to know, including a class file you can use straight away:

这篇文章有你需要知道的所有内容,包括你可以立即使用的类文件:

http://www.markj.net/iphone-asynchronous-table-image/

#1


I had the same problem, where my TableView would have to download each image before it displayed the cell. Like you say, it causes a big slowdown in the scrolling speed. What you need to do is download the images in the background and then insert them into the cell when they're finished downloading. This is how the app store app does it.

我有同样的问题,我的TableView必须在显示单元格之前下载每个图像。就像你说的那样,它会导致滚动速度大幅下降。您需要做的是在后台下载图像,然后在下载完成后将它们插入到单元格中。这就是应用程序商店应用程序的功能。

This post has all you need to know, including a class file you can use straight away:

这篇文章有你需要知道的所有内容,包括你可以立即使用的类文件:

http://www.markj.net/iphone-asynchronous-table-image/