2014-03-07 77 views
0

我有問題從服務器下載圖像陣列,如果我有100個圖像在服務器上意味着我應該下載所有圖像使用「AFImageRequestOperation」,而下載過程中一些圖像下載成功,但許多圖像無法下載,因爲「超時」從服務器錯誤,AFImageRequestOperation獲取「請求超時。」錯誤

我使用follwing方式來下載圖像:

NSURL *url = [NSURL URLWithString:kBaseURLString]; 
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; 
[httpClient setAuthorizationHeaderWithUsername:[[NSUserDefaults standardUserDefaults] stringForKey:kUserDefaultKeyUsername] 
             password:[[NSUserDefaults standardUserDefaults] stringForKey:kUserDefaultKeyPassword]]; 

for(int i = 0; i < [self.downloadImageList count]; i++) { 

    NSString *filename = [[NSString alloc] initWithString:[self.downloadImageList objectAtIndex:i]]; 
    NSMutableURLRequest *urlRequest = [httpClient multipartFormRequestWithMethod:@"POST" 
                      path:@"/xxx/yyyyyyyyyy/getImage" 
                     parameters:nil 
                 constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) { 

                  [formData appendPartWithFormData:[filename dataUsingEncoding:NSUTF8StringEncoding] 
                         name:kFormNameFile]; 

                 }]; 

    AFImageRequestOperation *requestOperation = [[AFImageRequestOperation alloc] initWithRequest:urlRequest]; 
    [requestOperation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) { 

     [self updateSyncClientUIDelegateProgress:(totalBytesRead/totalBytesExpectedToRead) andLabel:@"Downloading Images"]; 
    }]; 

    [requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 


     if([[urlRequest URL] isEqual:[[operation request] URL]]) { 

      dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 
       UIImage *image = responseObject; 
       NSLog(@"Downloading image %@",image); 

       [UIImagePNGRepresentation(image) writeToFile:[syncedImagesPath stringByAppendingPathComponent:filename] atomically:YES]; 
      }); 

     } 
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

     if([[operation response] statusCode] == 404) { 
      return; 
     } 
     NSLog(@"failure BLOCK %@",error); 
     NSLog(@"failure error code %ld",(long)[error code]); 



     if([error code] != NSURLErrorCannotDecodeContentData) { 

      [self cancelSyncFromFailure]; 
     } 

    }]; 

終於i接收錯誤(NSURLErrorDomain代碼= -1001‘請求超時’)對於許多圖像請求,

Error Report: Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x155ef3b0 {NSErrorFailingURLStringKey=http://integration.xxx.com/yyy/zzz/getImage, NSErrorFailingURLKey=http://xxxx.yyy.com/xxx/yyyyy/getImage, NSLocalizedDescription=The request timed out., NSUnderlyingError=0x1683e130 "The request timed out."} 

請幫我弄清楚這個問題,我應該怎麼計算策略這個問題,謝謝

+0

喜還是IM卡之前添加此行有這個問題,任何人都可以幫我解決這個問題 – karthick

回答

0

如果鋼材有問題,嘗試增加超時值,創造requestOperation

[urlRequest setTimeoutInterval:120]; // increase if use slow network/server