UIImagePickerController返回的图像超出了图像边缘,添加了黑色条纹。

3

我让用户从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


我会将问题隔离到UIImage或UIImageView上。在testImageView.image = image处设置断点,快速查看image以查看是否有黑色条纹。 - othomas
你找到解决方案了吗?我也遇到了同样的问题。 - Rafaela Lourenço
我的问题和这个一样,我找到了一些解决方案,但对我没有用。https://dev59.com/P2PVa4cB1Zd3GeqP65J-#13813738 - Manish Nahar
@EmanHarout,你找到这个问题的解决方案了吗?目前,我在iPhone 6 Plus和iPhone X上也遇到了同样的问题。但是在其他一些设备上它可以正常工作。 - Rajinderpal Singh
不幸的是,我从未找到解决办法。希望我有更好的消息。 - Eman Harout
显示剩余3条评论
1个回答

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
      }

 }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接