我有从服务器下载图像数组的问题,如果我在服务器上有100张图像意味着我应该使用“AFImageRequestOperation”下载所有图像,而下载过程中一些图像下载成功但很多图像因为“ TimeOut“来自服务器的错误,
我使用以下方式下载图像:
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];
}
}];
最后我收到许多图像请求的错误(NSURLErrorDomain Code = -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 :(得分:0)
如果钢铁有问题,请尝试增加超时值,在创建requestOperation
[urlRequest setTimeoutInterval:120]; // increase if use slow network/server