title: uploading an image in swift 4.2 with imagePickerController
body: <p>I would like to upload an image with ImagePickerController to a Rails Backend using the  in Swift 4.2 using Alamofire. The problem I am facing is that the image must only upload when pressing a register button. Below is my imagePickerController function. The Alamofire part should be done in the handleRegister function. The question is: how do I store the picked image in the local storage of the device (userDefaults) and use the Alamofire upload image in the handleRegister function?</p>
  
  <pre><code>func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
      if let image = info[.originalImage] as? UIImage {
          registrationViewModel.bindableImage.value = image
          let imageData = image.jpegData(compressionQuality: 0.5)
  
          Alamofire.upload(multipartFormData: { (multipartFormData) in
              multipartFormData.append(imageData!, withName: "account[user_attributes[profile_picture]]", fileName: "user_profile_picture.png", mimeType: "image/jpeg"); for(key, value) in self.parameters {
                  multipartFormData.append(value.data(using: String.Encoding.utf8)!, withName: key)
              }
          }, to: URL_REGISTER,
             headers: nil)
          {(result) in
              switch result {
              case .success(let upload, _, _):
                  upload.uploadProgress(closure: {(Progress) in
                      print("Upload progress: \(Progress.fractionCompleted)")
                  })
                  upload.responseJSON { response in
                      if let JSON = response.result.value {
                          print("JSON: \(JSON)")
                      }
                  }
              case .failure(let encodingError):
                  print(encodingError)
              }
          }
      } else {
      }
      dismiss(animated: true, completion: nil)
  }
  
  
  
  
  @objc fileprivate func handleRegister() {
      self.handleTapDismiss()
      guard let username = userNameTextField.text else {return}
      guard let email = emailTextField.text else {return}
      guard let password = passwordTextField.text else {return}
  
      registeringHUD.textLabel.text = "Register"
      registeringHUD.show(in: view)
  
  
      AuthService.instance.registerUser(email: email, password: password, username: username) { (success) in
          if success {
              print("user has signed up")
  
              // only upload images to backend once you are authorized
  
          } else {
              self.showHUDWithError()
          }
          self.dismiss(animated: true, completion: nil)
      }
  
  }
  </code></pre>