0
我相信我正在顛倒這樣做。我正在調用CGI腳本從服務器上的私人目錄中獲取大圖像。在下面的代碼中,我從服務器響應中檢索圖像。AFNetworking:從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];