2016-10-11 39 views
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]; 

回答

0

事實證明,AFNetworking有一個很好的數據任務的方法,允許在兩個監控進度,上傳和下載任務:

dataTaskWithRequest:上傳進度:downloadProgress:completionHandler:

唯一需要注意的是downloadProgress塊只有在響應頭中包含「Content-Length」時纔會更新。

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 
} 
相關問題