title: how to upload gallery image using alamofire in swift
body: <p>I want to upload image from with <code>imagePickerController</code> using <code>Alamofire</code> in swift.
   I have two functions one for original api call which is <code>ApplyLeave</code> and other function is for imageupload which is <code>imageUploadrequestWith</code>. In <code>ApplyLeave</code> function there is one parameter of image i want to pass for uploading which is <code>leave_certificate: gfg</code>, this <code>leave_certificate</code> paramter should take parameter for image uploading. i have seprete function for image uploading how to use it as parameter in <code>ApplyLeave</code> function, and I have <code>imagePickerController</code> in seprete class. I really confused how to use this and how to merge all stuff.
          Please see my folllowing code.</p>
  
  <pre class="lang-swift prettyprint-override"><code>class APIserviceprovider {
  
      static func ApplyLeave (
      startDate:String,
      endDate:String,
      description:String,
      reason:String,
      completion:@escaping (_ isSuccesfull :Bool, _ errorMessage :String?) -&gt;()
      ) {
          guard let company_id = UserDefaults.standard.value(forKey: KeyValues.companyidKey) else {return}
          guard let user_id = UserDefaults.standard.value(forKey: KeyValues.userIdkey) else {return}
          guard let workspace_id = UserDefaults.standard.value(forKey: KeyValues.workspaceIdKey) else {return}
  
          let urlString = "\(ApiServiceProvider.BASE_URL + ApiServiceProvider.APPLY_LEAVE)"
          let parameters = [
              "leave_start_date": "\(startDate)",
              "leave_end_date": "\(endDate)",
              "leave_reason": "\(reason)",
              "leave_description": "\(description)",
              "user_id": "\(user_id)",
              "workspace_id": "\(workspace_id)",
              "company_id": "\(company_id)" ,
              "leave_certificate": "gfg"
          ]
  
      print(parameters)
      Alamofire.request(urlString, method:.post, parameters: parameters, encoding: URLEncoding.default).validate().responseJSON
          {
          response in
              switch response.result {
              case .failure(let error):
                  //completion(false, "Failed to Sign up")
                  print(error)
                  completion(false, "You failed to apply for leave.")
              case .success(let responseObject):
                  //print("response is success:  \(responseObject)")
                  completion(true, "You have successfully appplied for leave.")
                  if let JSON = response.result.value {
                      let result = JSON as! NSDictionary
                      print(result)
                  }
              }
          }
      }
  
  
      static func imageUploadrequestWith(
          endUrl: String,
          imageData: Data?,
          parameters: [String : Any],
          onCompletion: ((JSON?) -&gt; Void)? = nil,
          onError: ((Error?) -&gt; Void)? = nil
      ) {
          let url = "\(ApiServiceProvider.BASE_URL + ApiServiceProvider.APPLY_LEAVE)"
  
          let headers: HTTPHeaders = [
              /* "Authorization": "your_access_token",  in case you need authorization header */
              "Content-type": "multipart/form-data"
          ]
  
          Alamofire.upload(multipartFormData: { (multipartFormData) in
              for (key, value) in parameters {
                  multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
              }
  
              if let data = imageData{
                  multipartFormData.append(data, withName: "image", fileName: "image.png", mimeType: "image/png")
              }
  
          }, usingThreshold: UInt64.init(), to: url, method: .post, headers: headers) { (result) in
              switch result{
              case .success(let upload, _, _):
                  upload.responseJSON { response in
                      print("Succesfully uploaded")
                      if let err = response.error{
                          onError?(err)
                          return
                      }
                      onCompletion?(nil)
                  }
              case .failure(let error):
                  print("Error in upload: \(error.localizedDescription)")
                  onError?(error)
              }
          }
      }
  
  }
  
  class ApplyLeaveController: UIViewController, UITextFieldDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate {
  
      func textFieldDidBeginEditing(_ textField: UITextField) {
          if textField == self.uploadFileButton {
  
              let myPickerController = UIImagePickerController()
              myPickerController.delegate = self;
              myPickerController.sourceType =  UIImagePickerControllerSourceType.photoLibrary
              self.present(myPickerController, animated: true, completion: nil)
          }
      }
  
      @objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
      {
          let image_data = info[UIImagePickerControllerOriginalImage] as? UIImage
          let imageData:Data = UIImagePNGRepresentation(image_data!)!
          let imageStr = imageData.base64EncodedString()
          uploadFileButton.text = imageStr
          self.dismiss(animated: true, completion: nil)
      }
  }
  </code></pre>