2014-03-07 74 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