2012-02-06 101 views
1

大家好,早上好。我正在使用asihttprequest进行一系列图像下载 - 来自服务器的4个图像。但是,我注意到一个问题并找不到解决方案。假设我正在通过URL下载4张图片,并且万一有一张或两张图片不可用,它将取消整个队列。asihttprequest图片下载

这里是我的代码:

[networkQueue setDownloadProgressDelegate:progressIndicator]; 
    [networkQueue setRequestDidFinishSelector:@selector(imageFetchComplete:)]; 
    [networkQueue setRequestDidFailSelector:@selector(imageFetchFailed:)]; 

    request = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:@"http://imagees/image1.jpg"]]; 
    [request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"1.png"]]; 
    [request setDownloadProgressDelegate:imageProgressIndicator1]; 
    [request setUserInfo:[NSDictionary dictionaryWithObject:@"request1" forKey:@"name"]]; 
    [networkQueue addOperation:request]; 

    request = [[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"sdvdsvsadvsadv"]] autorelease]; 
    [request setDownloadDestinationPath:[[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"2.png"]]; 
    [request setDownloadProgressDelegate:imageProgressIndicator2]; 
    [request setUserInfo:[NSDictionary dictionaryWithObject:@"request2" forKey:@"name"]]; 
    [networkQueue addOperation:request]; 

- (void)imageFetchComplete:(ASIHTTPRequest *)request 
{ 
    UIImage *img = [UIImage imageWithContentsOfFile:[request downloadDestinationPath]]; 
    if (img) { 
     if ([imageView1 image]) { 
      if ([imageView2 image]) { 
       [imageView3 setImage:img]; 
      } else { 
       [imageView2 setImage:img]; 
      } 
     } else { 
      [imageView1 setImage:img]; 
     } 
    } 
} 

- (void)imageFetchFailed:(ASIHTTPRequest *)request 
{ 
    if (!failed) { 
     if ([[request error] domain] != NetworkRequestErrorDomain || [[request error] code] != ASIRequestCancelledErrorType) { 
      UIAlertView *alertView = [[[UIAlertView alloc] initWithTitle:@"Download failed" message:@"Failed to download images" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease]; 
      [alertView show]; 
     } 
     failed = YES; 
    } 
} 

的问题是,假设取第二图像失败,它会显示错误消息并停止整个操作,尽管图片1是一个有效的图像文件。

任何帮助将不胜感激。

:)

回答

5

ASIHTTPRequest documentation

当在ASINetworkQueue请求失败,队列将默认 取消所有其他请求。您可以通过[队列 setShouldCancelAllRequestsOnFailure:NO]来禁用此行为。

+0

非常感谢jonkroll :) – Veer 2012-02-06 20:44:25