从UIImagePickerController返回的图像超出了图像的边缘,并添加了黑条

时间:2018-08-29 00:18:09

标签: ios uiimagepickercontroller

我让用户从UIImagePickerController中选择一个图像,当我以后访问该图像时,它有一个黑条。请注意,图像没有被裁剪,图像选择器实际上超出了不存在像素的图像范围,并添加了空白。

我的代码:

@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)
}

结果如下:

enter image description here

请注意,我已经尝试过更改图像视图的contentMode的比例以进行填充和填充(不适合)。我不知道为什么将黑条添加为图像本身的一部分。有人可以协助吗? enter image description here

1 个答案:

答案 0 :(得分:0)

如果裁剪后的图像在原点之外,我已经通过使用原始图像解决了这个问题

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
      }

 }