简体   繁体   中英

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

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:

在此处输入图片说明

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? 在此处输入图片说明

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
      }

 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM