我有从服务器下载图像数组的问题,如果我在服务器上有100个图像意味着我应该使用" AFImageRequestOperation"下载所有图像,在下载过程中一些图像被成功下载但很多图像都失败了下载因为" TimeOut"来自服务器的错误,我面临大尺寸图像(3.mb)的超时问题,
我使用以下方式下载图像:
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];
}
}];
请在下载大型iamges时帮我修复此超时问题
答案 0 :(得分:1)
现在这是我正在使用和工作的解决方案.FIRSTLY它也是发送给我&#34; 请求超时&#34;消息但不是现在这个
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
}];
答案 1 :(得分:0)
您设置TimeOut请求时间更多是以下代码。
[urlRequest setTimeoutInterval:50];