Image returned from UIImagePickerController goes beyond edge of image, adds black bar

Viewed 232

I have users select an image from the UIImagePickerController and when I access the image afterwards, it has a black bar. Note the image is not cropped, the image picker actually goes beyond the bounds of the image where no pixels exist and adds empty blackspace.

My code:

@IBAction func selectImageButt(_ sender: Any) {
    let imagepicker = UIImagePickerController()
    imagepicker.allowsEditing = true
    imagepicker.sourceType = .photoLibrary
    imagepicker.delegate = self
    present(imagepicker, animated: true)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerEditedImage] as? UIImage {
        testImageView.image = image
    }
    picker.dismiss(animated: true)
}

The results are as follows:

enter image description here

Note I've tried changing the image view's contentMode to scale to fill as well as aspect fill (not fit). I don't know why the black bars are added as part of the image itself. Can anybody assist? enter image description here

1 Answers

I've resolved this issue by use original image if the cropped image is out side the origin

guard let selectedImage = info[.editedImage] as? UIImage else {
        fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
    }

var image = selectedImage
 if let cropRect = info[.cropRect] as? CGRect {
     if cropRect.origin.y < 0 {
          guard let imageOrigin = info[.originalImage] as? UIImage else {
              fatalError("Expected a dictionary containing an image, but was provided the following: \(info)")
          }
          image = imageOrigin
      }

 }
Related