title: Upload Image in to server
body: <p>This my code from this link</p>
  
  <p><a href="https://stackoverflow.com/questions/11084403/uploading-image-via-post-in-objective-c">Uploading Image via POST in Objective C</a></p>
  
  <pre><code>// COnvert Image to NSData
  NSData *dataImage = UIImageJPEGRepresentation(_chosenImage, 1.0f);
  NSLog(@"%@", dataImage);
  NSString *tempURLString = [APIStr stringByReplacingOccurrencesOfString:@"***" withString:@"upload"];
  
  // set your URL Where to Upload Image
  NSString *urlString = @"My URL String";
  
  // set your Image Name
  NSString *filename = @"My File Name";
  
  // Create 'POST' MutableRequest with Data and Other Image Attachment.
  NSMutableURLRequest* request= [[NSMutableURLRequest alloc] init];
  [request setURL:[NSURL URLWithString:urlString]];
  NSLog(@"%@", request);
  [request setHTTPMethod:@"POST"];    
  NSString *boundary = @"---------------------------14737809831466499882746641449";
  NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
  [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
  NSMutableData *postbody = [NSMutableData data];
  [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"my_upload_file_name\"; filename=\"%@_.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
  NSLog(@"%@", postbody);
  [postbody appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  [postbody appendData:[NSData dataWithData:dataImage]];
  [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  [request setHTTPBody:postbody];
  
  // Get Response of Your Request
  NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
  NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
  NSLog(@"Response  %@",responseString);
  </code></pre>
  
  <p>This code not working for me.. I'm getting failed message. Is any wrong in my code.</p>
  
  <p>And i tried this code also</p>
  
  <p><a href="https://jmsliu.com/3241/select-and-upload-multiple-photos-from-gallery-or-camera-in-ios.html" rel="nofollow noreferrer">https://jmsliu.com/3241/select-and-upload-multiple-photos-from-gallery-or-camera-in-ios.html</a></p>
  
  <pre><code>- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  
  _chosenImage = info[UIImagePickerControllerEditedImage];
     [self uploadImageLegacy:_chosenImage];
  }
  
  
  - (void) uploadImageLegacy:(UIImage *)image {
  NSString *tempURLString = [Str stringByReplacingOccurrencesOfString:@"***" withString:@"upload"];
  //upload single image
  NSURL *url = @"My URL here";
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
  [request setHTTPMethod:@"POST"];
  NSString *POST_BODY_BOURDARY = @"SportuondoFormBoundary";
  NSString *contentTypeValue = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", POST_BODY_BOURDARY];
  [request addValue:contentTypeValue forHTTPHeaderField:@"Content-type"];
  
  NSMutableData *dataForm = [NSMutableData alloc];
  [dataForm appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",POST_BODY_BOURDARY] dataUsingEncoding:NSUTF8StringEncoding]];
  [dataForm appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"param1\";\r\n\r\n%@", @"10001"] dataUsingEncoding:NSUTF8StringEncoding]];
  
  NSData *imageData = UIImageJPEGRepresentation(image, 0.9);
  [dataForm appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",POST_BODY_BOURDARY] dataUsingEncoding:NSUTF8StringEncoding]];
  [dataForm appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"my_upload_file_name\"; filename=\"%@.jpg\"\r\n", @"test"] dataUsingEncoding:NSUTF8StringEncoding]];
  [dataForm appendData:[[NSString stringWithFormat:@"Content-Type: image/jpeg\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [dataForm appendData:[NSData dataWithData:imageData]];
  
  [dataForm appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",POST_BODY_BOURDARY] dataUsingEncoding:NSUTF8StringEncoding]];
  [request setHTTPBody:dataForm];
  }
  
  - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend {
  NSLog(@"Sent %lld, Total sent %lld, Not Sent %lld", bytesSent, totalBytesSent, totalBytesExpectedToSend);
  }
  
  - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response completionHandler:(void (^)(NSURLSessionResponseDisposition))completionHandler {
  _receiveData = [NSMutableData data];
  [_receiveData setLength:0];
  completionHandler(NSURLSessionResponseAllow);
  NSLog(@"NSURLSession Starts to Receive Data");
  }
  
  - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data {
  [_receiveData appendData:data];
  NSLog(@"NSURLSession Receive Data");
  }
  
   - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
  NSLog(@"URL Session Complete: %@", task.response.description);
  
  if(error != nil) {
      NSLog(@"Error %@",[error userInfo]);
  } else {
      NSLog(@"Uploading is Succesfull");
  
      NSString *result = [[NSString alloc] initWithData:_receiveData encoding:NSUTF8StringEncoding];
      NSLog(@"%@", result);
  }
  }
  </code></pre>