在iOS中优化多个异步图像下载 [英] optimising multiple asynchronous image downloads in iOS

查看:96
本文介绍了在iOS中优化多个异步图像下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从服务器下载很多图片.我如何尽快做到这一点?目前我正在使用:

I want to download a lot of pictures from the server. How can I do this as fast as possible? Currently I am using:

UIImage* myImage = [UIImage imageWithData: 
[NSData dataWithContentsOfURL: 
[NSURL URLWithString: @"http://example.com/image.jpg"]]];

这太慢了.同时(异步)下载多张图片是否有速度提高?如果有的话,太多了吗?

It is painfully slow. Is there any speed increase in downloading multiple images at the same time (asynchronously), and if so how many is too many?

推荐答案

您将无法获得最佳连接数的肯定答案,因为没有连接数.它仅取决于几个变量,例如带宽,图像大小或您自己的耐心.您需要自己进行测量以使其正确.

You won't get a definitive answer to the optimal number of connections, because there is none. It just depends on several variables such as bandwidth, image size or your own patience. You need to measure this by yourself to get it right.

执行异步请求不会提高下载速度,但用户体验会更好.认真地讲,您应该考虑对所有耗时超过一秒的下载执行此操作.

Doing asynchronous requests won't increase the downloading speed, but the user experience is way better. Seriously, you should consider doing it for any download that takes more than a second.

我总是建议使用 ASIHTTPRequest ,它可以实现诸如

I always recommend using ASIHTTPRequest, it makes implementing things such as queues and progress bars easy.

这是使用该库的异步请求中最简单的示例:

Here's the simplest example from an asynchronous request using the library:

- (IBAction)grabURLInBackground:(id)sender
{
   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setDelegate:self];
   [request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
   // Use when fetching text data
   NSString *responseString = [request responseString];

   // Use when fetching binary data
   NSData *responseData = [request responseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
   NSError *error = [request error];
}

更新:将不再支持该库.来自 1 :

Update: This library will no longer be supported. From 1:

"请注意,我不再使用该库-您可以 想考虑为新项目使用其他东西. :)"

"Please note that I am no longer working on this library - you may want to consider using something else for new projects. :)"

如今,我的大多数项目都使用 AFNetworking .

Nowadays I use AFNetworking for most of my projects.

这篇关于在iOS中优化多个异步图像下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆