2012-02-06 106 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