title: Cropping an image with imagepickercontroller in swift
body: <p>I am currently making a program in swift that involves a screen of choosing an image from either camera or photo library using action sheet. This is fully functional however I would like to be able to choose a square section from the selected image, similar to apple default apps. How can I implement this? Here is my functional code:</p>
  
  <pre><code>func chooseImage(_ sender: Any) {
  
      let imagePickerController = UIImagePickerController()
      imagePickerController.delegate = self
  
      let actionSheet = UIAlertController(title: "Photo Source", message: "Choose a source", preferredStyle: .actionSheet)
  
      actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action:UIAlertAction) in
  
          if UIImagePickerController.isSourceTypeAvailable(.camera) {
              imagePickerController.sourceType = .camera
              self.present(imagePickerController, animated: true, completion: nil)
          }else{
              print("Camera not available")
          }
  
  
      }))
  
      actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action:UIAlertAction) in
          imagePickerController.sourceType = .photoLibrary
          self.present(imagePickerController, animated: true, completion: nil)
      }))
  
      actionSheet.addAction(UIAlertAction(title: "Default", style: .default, handler: { (action:UIAlertAction) in
          self.avatarImageView.image = UIImage(named: "Avatar.png")
  
  
      }))
  
      actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
  
      self.present(actionSheet, animated: true, completion: nil)
  
  
  }
  
  func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
      let image = info[UIImagePickerControllerOriginalImage] as! UIImage
  
      avatarImageView.image = image
  
      picker.dismiss(animated: true, completion: nil)
  }
  
  func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
      picker.dismiss(animated: true, completion: nil)
  }
  // Saves the User singleton object onto the device
  static func saveData() {
      let savedData = NSKeyedArchiver.archivedData(withRootObject: User.sharedUser)
      UserDefaults.standard.set(savedData, forKey: "user")
  }
  </code></pre>