AFNetworking:从 CGI 脚本中检索大图像,跟踪服务器响应
Posted
技术标签:
【中文标题】AFNetworking:从 CGI 脚本中检索大图像,跟踪服务器响应【英文标题】:AFNetworking: retrieve large image from CGI script, tracking server response 【发布时间】:2016-10-11 03:31:23 【问题描述】:我确定我是颠倒过来的。我正在调用 CGI 脚本从服务器上的私有目录中获取大图像。在下面的代码中,我从服务器响应中检索图像。
一切正常,但我想监控响应进度以向用户提供一些反馈(我正在避免下载,因为从本地磁盘恢复图像需要额外的时间)。
uploadProgress 跟踪发送到服务器的数据。有什么方法可以监控服务器的响应吗?看起来 responseObject 只能在完成处理程序中访问。
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://www.myurl.com/redirect.html" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
NSLog(@"Calling server");
[formData appendPartWithFormData:[user_id dataUsingEncoding:NSUTF8StringEncoding] name:@"user"];
[formData appendPartWithFormData:[_imageURL dataUsingEncoding:NSUTF8StringEncoding] name:@"filename"];
error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager uploadTaskWithStreamedRequest:request progress:^(NSProgress * _Nonnull uploadProgress)
dispatch_async(dispatch_get_main_queue(), ^
NSLog(@"SERVER UPLOAD FRACTION COMPLETED: %f", uploadProgress.fractionCompleted);
);
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error)
NSLog(@"Error: %@", error);
NSLog(@"RESPONSE: %@", response);
NSLog(@"responseObject: %@", responseObject);
if (error)
NSLog(@"Error: %@", error);
else
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^
UIImage *image = [UIImage imageWithData:responseObject];
dispatch_sync(dispatch_get_main_queue(), ^
[myImageView setImage:image];
);
);
];
[uploadTask resume];
【问题讨论】:
【参考方案1】:事实证明,AFNetworking 有一个很好的数据任务方法,可以监控上传和下载任务的进度:
dataTaskWithRequest:uploadProgress:downloadProgress:completionHandler:
唯一需要注意的是,仅当响应标头中存在“Content-Length”时才会更新 downloadProgress 块。
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:urlString parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
[formData appendPartWithFormData:[imageURL dataUsingEncoding:NSUTF8StringEncoding] name:@"filename"];
error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSURLSessionDataTask *uploadTask = [manager dataTaskWithRequest:request uploadProgress:^(NSProgress * _Nonnull uploadProgress)
dispatch_async(dispatch_get_main_queue(), ^
NSLog(@"SERVER UPLOAD FRACTION COMPLETED: %f", uploadProgress.fractionCompleted);
);
downloadProgress:^(NSProgress * _Nonnull downloadProgress)
dispatch_async(dispatch_get_main_queue(), ^
NSLog(@"SERVER DOWNLOAD FRACTION COMPLETED: %f", downloadProgress.fractionCompleted);
);
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error)
// retrieve the image from the responseObject here
【讨论】:
以上是关于AFNetworking:从 CGI 脚本中检索大图像,跟踪服务器响应的主要内容,如果未能解决你的问题,请参考以下文章
UIImageView+AFNetworking 未从服务器检索最新图像
iOS Objective-C AFNetworking dataTaskWithRequest:completionHandler:无法从完成块内部检索数据