为什么completionHandler回调被称为延迟然后AFNetworking上传多个图像的进度(它的1.0)

时间:2016-06-21 09:21:38

标签: ios objective-c iphone afnetworking-3

我使用AFNetworking3.0在iOS上传一些图像,代码如下所示, images 是UIImage的数组。 我有两个问题:

  1. 在进度块中,当uploadProgress.fractionCompleted等于1.0时打印日志A,在completionHandler块的头部打印日志B,然后我找到了日志A的时间和B差别很大。有什么不对或正常吗?

  2. 当我上传多张图片时,为了获得更好的性能和更少的上传时间,我应该创建多个请求或者如下所示,这是正确的选择吗?

  3. 代码如下:

    NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:URL_IMAGE_SERVER parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {           
      for(UIImage *eachImage in images) {
        NSData *imageData = UIImageJPEGRepresentation(eachImage, 0.5);
        [formData appendPartWithFileData:imageData name:@"upload_picture" fileName:@"filename.jpg" mimeType:@"image/jpeg"];
      }
    } error:nil];
    AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSURLSessionUploadTask *uploadTask;
    uploadTask = [manager
      uploadTaskWithStreamedRequest:request
      progress:^(NSProgress * _Nonnull uploadProgress) {
         // This is not called back on the main queue.
         // You are responsible for dispatching to the main queue for UI updates
          dispatch_async(dispatch_get_main_queue(), ^{
       });
     }
      completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
         if (error) {
         } else {
         }
      }];
    [uploadTask resume];
    

0 个答案:

没有答案