我确信我这样做是颠倒的。我正在调用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];
答案 0 :(得分:0)
事实证明,AFNetworking有一个很好的数据任务方法,可以监控上传和下载任务的进度:
dataTaskWithRequest:上传进度: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
}