gpt4 book ai didi

ios - 如何在 AFImageRequestOperation 中设置超时值

转载 作者:行者123 更新时间:2023-11-29 03:06:36 24 4
gpt4 key购买 nike

我在从服务器下载图像数组时遇到问题,如果我在服务器上有 100 张图像意味着我应该使用“AFImageRequestOperation”下载所有图像,在下载过程中一些图像已成功下载但许多图像由于“服务器超时”错误,我面临大尺寸图像 (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];
}

}];

请帮我解决这个下载大图片时超时的问题

最佳答案

现在这是我正在使用并且工作正常的解决方案。首先它还向我发送了“请求超时”消息,但不是现在。试试这个

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
}];

关于ios - 如何在 AFImageRequestOperation 中设置超时值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22705055/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com