设计异步下载程序显示图像

发布于 2024-12-12 17:21:34 字数 374 浏览 0 评论 0原文

我有一个 JSON feed,其中包含图像的 URL。我正在使用 NSURLConnection 下载 JSON feed 并提取 URL。我想异步下载所有图像。我对 UIImage 进行了子类化,并向该类发送了一个 URL,它以异步方式一次下载一张图像。

首先,这是一个好方法吗?其次,我想一次显示四张图像。难道我不应该把每组四个一起下载,而不是一个一个地下载吗?

我的第二个问题是我有两个NSURLConnection。这可能很糟糕。我是否应该使用相同的 NSURLConnection 来下载 JSON feed 并同时获取图像?

我试图一次显示四个图像,并使用下一个按钮在下一行显示接下来的四个图像。

I have a JSON feed which contains URLs for images. I am using NSURLConnection to download the JSON feed extract the URLs. I want to download all the images asynchronously. I subclassed UIImage and sent that class a URL which it downloads, one image at at a time, in an asynchronous manner.

First, is that a good way to do it? Second, I'd like to show four images at a time. Shouldn't I download every set of four together instead of downloading one by one?

My second concern is that I have twoNSURLConnections. That's probably bad. Should I use the very same NSURLConnection to download the JSON feed and at the same time get the image?

I am trying to display four images at a time, with a next button that displays the next four on the next line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溇涏 2024-12-19 17:21:34

我不确定 UIImage 是否应该被子类化。如果您需要良好的设计实践,您应该看看 TopPaid
来自 Apple 的示例代码,它展示了如何正确下载 feed,然后异步下载图像。仔细查看该项目中的 IconDownloader 类,它是一个处理图像下载的类,并在完成时通知其委托。

据我所知,一次拥有多个 NSURLConnection 没有问题。如果连接数量变得非常,您可能会遇到麻烦,因为这可能会使 iPhone 上允许的打开文件描述符数量饱和,或者更可能会产生内存警告。就您的情况而言,如果您只有 2 个连接,则不会有任何问题。

I am not sure UIImage is expected to be subclassed. If you need good design practice, you should have a look at TopPaid
sample code from Apple, as it is showing how to properly download a feed and then asynchronously download images. Take a close look at the IconDownloader class from this project, which is a class handling image downloading, and notifying its delegate when it's finished.

As far as I know there is no problem with having multiple NSURLConnections at a time. You might run into trouble if the number of connections becomes very large, because this could saturate the number of open file descriptors allowed on iPhone, or more likely create a memory warning. In your case if you only have 2 connections, you don't have any problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文