目标c中一次出现多个异步请求

我正在同时从Web服务器下载多个图像,我的问题是它们彼此混淆了。

dispatch_async(dispatch_get_global_queue(0,0), ^{ NSData * data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"website.com"]]; if (data == nil) return; dispatch_async(dispatch_get_main_queue(), ^{ cell.imageView.image = [UIImage imageWithData:data]; }); }); 

我得到了以下代码: 从URL Objective C获取图像 。

我该如何解决?? 还有什么办法可以加快下载速度吗?

您可以使用“AsyncImageView”类文件同步加载图像,并在图像加载时显示活动指示器

AsyncImageView是一个类文件,它将为每个调用创建连接,当图像数据下载完成后,它将返回imageview的图像。 如果图像已经在缓存中,则只返回图像而不创建连接。

您可以从以下链接下载“AsyncImageView”类文件: – https://www.dropbox.com/s/peazwjvky9fsjd7/Archive.zip

在.m文件中导入AsyncImageView类

  #import "AsyncImageView.h" 

在indexpath方法的tableview单元格中

 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *CellIdentifier = @"SimpleTableCell"; UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(X, y, width, height)]; NSString *imageURL = [NSString stringWithFormat: @"www.xyz.image.png"]; AsyncImageView *async = [[AsyncImageView alloc]initWithFrame:CGRectMake(0, 0, width, height)]; [async loadImageFromURL:[NSURL URLWithString:imageURL]]; [imageView addSubview:async]; [cell addSubview:imageView]; return cell; } 

试试这个你的问题会解决。