0

我使用AFMultipartFormData發送多張圖片到服務器使用一個for循環在時間獲取一幅圖像:運行的方法時AFMultipartFormData完成上傳

NSString *string = [NSString stringWithFormat:@"%@/API/Upload",BaseURLString]; 
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 
    [manager setRequestSerializer:[AFHTTPRequestSerializer serializer]]; 
    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]]; 
    NSError *error; 
    NSString *mystring = @"noName"; 
//FOR Loop Start 
    for(NSData *eachImage in dataStringArray) { 

     NSURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:string parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 

      [formData appendPartWithFormData:eachImage name:@"myImage"]; 
      [formData appendPartWithFormData:[mystring dataUsingEncoding:NSUTF8StringEncoding] 
             name:@"FileName"]; 
     } error:&error]; 

     NSURLSessionDataTask *task = [manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { 
      if (error) { 
       NSLog(@"%@", error.localizedDescription); 
       return; 
      } 

      [uploadedImageIDs addObject:[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]]; 

     }]; 
     [task resume]; 
    }//for end 
     [self makeJSON:uploadedImageIDs]; 

但不知何故,它之前調用makeJSON方法。由於有多張圖片,因此我需要在所有圖片的上傳完成後調用。

+0

只需在塊內調用[self makeJSON:uploadedImageIDs]; –

回答

1

網絡處理將通過異步運行,因此您需要在網絡completionHanlder中進行計數,以瞭解所有任務何時完成。像這樣

NSString *string = [NSString stringWithFormat:@"%@/API/Upload",BaseURLString]; 
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager]; 
    [manager setRequestSerializer:[AFHTTPRequestSerializer serializer]]; 
    [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]]; 
    NSError *error; 
    NSString *mystring = @"noName"; 
    NSInteger count = 0; 

//FOR Loop Start 
    for(NSData *eachImage in dataStringArray) { 

     NSURLRequest *request = [manager.requestSerializer multipartFormRequestWithMethod:@"POST" URLString:string parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 

      [formData appendPartWithFormData:eachImage name:@"myImage"]; 
      [formData appendPartWithFormData:[mystring dataUsingEncoding:NSUTF8StringEncoding] 
             name:@"FileName"]; 
     } error:&error]; 

     NSURLSessionDataTask *task = [manager dataTaskWithRequest:request uploadProgress:nil downloadProgress:nil completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { 
      count++; 

      if (error) { 
       NSLog(@"%@", error.localizedDescription); 
      } else { 
       [uploadedImageIDs addObject:[[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding]]; 
      } 

      if (count == dataStringArray.count) { 
       [self makeJSON:uploadedImageIDs]; 
      } 
     }]; 
     [task resume]; 
    }//for end