简体   繁体   English

设计用于下载和显示图像的异步过程

[英]Designing asynchronous procedure for downloading & displaying images

I have a JSON feed which contains URLs for images. 我有一个JSON供稿,其中包含图像的URL。 I am using NSURLConnection to download the JSON feed extract the URLs. 我正在使用NSURLConnection下载JSON feed提取URL。 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. 我将UIImage子类化,并发送给该类一个URL,它以异步方式一次下载一个图像。

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 two NSURLConnection s. 我的第二个担心是我有两个NSURLConnection That's probably bad. 那可能很糟糕。 Should I use the very same NSURLConnection to download the JSON feed and at the same time get the image? 是否应该使用相同的NSURLConnection下载JSON feed并同时获取图像?

I am trying to display four images at a time, with a next button that displays the next four on the next line. 我正在尝试一次显示四张图像,并用next按钮在下一行显示下四张图像。

I am not sure UIImage is expected to be subclassed. 我不确定UIImage是否应该被子类化。 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. 如果您需要良好的设计实践,则应查看Apple的TopPaid示例代码,因为它显示了如何正确下载提要,然后异步下载图像。 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. 请仔细查看该项目中的IconDownloader类,该类是处理图像下载的类,并在完成时通知其委托。

As far as I know there is no problem with having multiple NSURLConnections at a time. 据我所知,一次具有多个NSURLConnections没有问题。 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. 如果连接的数量变得非常大,你可能会遇到麻烦,因为这可能使饱和iPhone上打开的文件描述符的数量,或者更可能创建一个内存警告。 In your case if you only have 2 connections, you don't have any problem. 如果您只有2个连接,则没有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM