Ios 在目标c中同时有多个异步请求

Ios 在目标c中同时有多个异步请求,ios,objective-c,image,asynchronous,dispatch-async,Ios,Objective C,Image,Asynchronous,Dispatch Async,我同时从一个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_

我同时从一个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];
    });
});
我从以下位置获得此代码:

我该如何解决这个问题??还有什么方法可以加快下载速度吗?

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


AsyncImageView是一个类文件,它将在其中为每个调用创建连接,当图像数据下载完成时,它将返回imageview的图像。若映像已经在缓存中,那个么只需返回映像而不创建连接

您可以从以下链接下载“AsyncImageView”类文件:-

在.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;
}

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

除非你发布(剩下的)代码,否则没有人能帮助你。这个代码是如何调用的?什么是
单元
?不要介意,您最好使用NSURLConnection来控制缓存之类的功能。希望Pratik的回答能帮助您,但上面代码的问题包括1。您没有进行检查,以确保在图像加载完成时,单元格没有被滚动并重新用于表中的另一行;二,。如果在开始异步检索之前未使用占位符初始化单元格图像,则会看到前一行中使用此单元格的图像。出于性能原因,您还需要缓存映像(到RAM和持久存储)。使用经过验证的第三方库可能是最容易的。这很好,但是你能解释一下你刚才粘贴在这里的代码吗,或者,您只是链接到一些最终会死掉并且不会帮助任何人的代码?AsyncImageView是一个类文件,它将在其中为每个调用创建连接,当图像数据下载完成时,它将返回imageview的图像。若映像已经在缓存中,那个么只需返回映像而不创建连接,并用它更新您的答案。这里的解释是反对票和赞成票之间的区别。