title: iOS Developer: the network Api Issue. How to put a image data in a dictionary? How to persuade the server?
body: <p>iOS develop:  to upload images with POST network request.</p>
  
  <p>I use AFN .Usually we configurate the HTTP headers and put the image data in the HTTP body.</p>
  
  <p>After a lot googling , still not aware of it.</p>
  
  <p>The server, JAVA, demands the image data shall be uploaded with a key.</p>
  
  <p>I met a problem.</p>
  
  <p><a href="https://i.stack.imgur.com/153Vs.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/153Vs.png" alt="enter image description here"></a> </p>
  
  <pre><code> [formData appendPartWithFileData: imageData name: @"photoFile" fileName:[NSString stringWithFormat:@"%@%@.jpeg",dateString,@(idx)] mimeType:@"image/jpeg"];
  //the name is the key to the server.
  </code></pre>
  
  <p>So I set the <strong>name</strong>, after the server's confirming , I uploaded the image data successfully.</p>
  
  <p><strong>But AFN Error:</strong> </p>
  
  <blockquote>
    <p>domain: @"com.alamofire.error.serialization.response"</p>
  </blockquote>
  
  <p>I tried to fix it, and failed.</p>
  
  <p>I handled the types in the <strong>AFN</strong> , but it does not work.</p>
  
  <pre><code> @implementation AFJSONResponseSerializer
   self.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript",@"text/html", nil];
  </code></pre>
  
  <p>I doesn't know much of the server, and has been confused by the Java Engineer.</p>
  
  <p>I feel like the <strong>key</strong> (photoFile) has been set explicitly.</p>
  
  <p>PS: How to persuade the the Java Engineer, uploading images without key-value.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>+ (void)startMultiPartUploadTaskWithURL:(NSString *)url
                             imagesArray:(NSArray *)images
                       parameterOfimages:(NSString *)parameter
                          parametersDict:(NSDictionary *)parameters
                        compressionRatio:(float)ratio
                            succeedBlock:(void(^)(id response, NSError *error ))succeedBlock
                             failedBlock:(void(^)(id response, NSError *error ))failedBlock
                     uploadProgressBlock:(void (^)(float, long long, long long))uploadProgressBlock{
  
      AFHTTPRequestSerializer * requestSerializer = [AFHTTPRequestSerializer serializer ];
  
      [requestSerializer setValue: @"Bearer 933d16fa-8aa8-4c48-a250-62f905276ab7"  forHTTPHeaderField:@"Authorization"];
      [requestSerializer setValue: @"b6862a23-f60c-4551-adcd-efa845d1511a"  forHTTPHeaderField:@"X-Request-ID"];
  
      requestSerializer.timeoutInterval = 120.f;
      NSMutableURLRequest * request = [requestSerializer  multipartFormRequestWithMethod: @"POST" URLString: url parameters: parameters constructingBodyWithBlock:^(id&lt;AFMultipartFormData&gt;  _Nonnull formData) {
          if (images.count &gt; 0) {
              //generate the image's name
              NSDate *date = [NSDate date];
              NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
              [formatter setDateFormat:@"yyyyMMddHHmmss"];
              NSString *dateString = [formatter stringFromDate:date];
              [images enumerateObjectsUsingBlock:^(UIImage *image, NSUInteger idx, BOOL * _Nonnull stop) {
                  NSData *imageData;
                  //adjust the imageData size
                  imageData = UIImageJPEGRepresentation(image,ratio);
                  CGFloat sizeOriginKB = imageData.length / 1024.0;
                  CGFloat resizeRate = ratio/2.0;
                  //adjust the imageData size , &lt;250Kb
                  while (sizeOriginKB &gt; 250 &amp;&amp; resizeRate &gt; 0.01) {
                      imageData = UIImageJPEGRepresentation(image,resizeRate);
                      sizeOriginKB = imageData.length / 1024.0;
                      resizeRate = resizeRate/2.0;
                  }
                  [formData appendPartWithFileData:imageData name: @"photoFile" fileName:[NSString stringWithFormat:@"%@%@.jpeg",dateString,@(idx)] mimeType:@"image/jpeg"];
              }];
          }
      } error: nil ];
      NSURLSessionUploadTask * uploadTask;
      AFURLSessionManager * manager = [[AFURLSessionManager alloc] initWithSessionConfiguration: [NSURLSessionConfiguration defaultSessionConfiguration]];
      uploadTask = [manager uploadTaskWithStreamedRequest: request progress:^(NSProgress * _Nonnull uploadProgress) {} completionHandler:^(NSURLResponse * _Nonnull response, id  _Nullable responseObject, NSError * _Nullable error) {
          succeedBlock(responseObject, nil );
          failedBlock(nil, error);
          //responseObject;
          //error;
      } ];
      [uploadTask resume ];
  }
  </code></pre>