2014-03-28 69 views
0

我有从服务器下载图像阵列的问题,如果我有100个图像在服务器上意味着我应该下载所有图像使用“AFImageRequestOperation”,而下载过程中一些图像下载成功,但许多图像被下载失败的,因为从服务器“超时”的错误,我用大尺寸图像(3.mb)面临超时问题,如何在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]; 
     } 

    }]; 

请帮我在下载大内存时修复此超时问题

回答

0

您设置超时请求时间更多的是下面的代码。

[urlRequest setTimeoutInterval:50]; 
+0

仍然面临着同样的问题的解决,实际上即时从服务器获取错误错误:错误域= NSURLErrorDomain代码= -1001“请求超时。” UserInfo = 0x12830130 {NSErrorFailingURLStringKey = http://xxxxx.com/yyyy/httpbroker/getImage,NSErrorFailingURLKey = http://xxxxx.com/yyyyy/httpbroker/getImage,NSLocalizedDescription =请求超时,NSUnderlyingError = 0xe351d60“请求超时。“}失败错误代码-1001 – karthick

1

现在,这里是我使用和工作FINE.FIRSTLY也有人寄来的“请求超时”的消息,但不NOW.TRY本

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:photourl] cachePolicy:NSURLCacheStorageAllowed timeoutInterval:10000]; 

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

    ...... YOUR CODE 
}];