OGeek|极客世界-中国程序员成长平台

标题: ios - 从 UIImagePickerController 返回的图像超出图像边缘,添加黑条 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:30
标题: 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

注意我已经尝试将 ImageView 的 contentMode 更改为缩放以填充以及方面填充(不适合)。我不知道为什么将黑条添加为图像本身的一部分。有人可以帮忙吗? enter image description here



Best Answer-推荐答案


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

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
      }

 }

关于ios - 从 UIImagePickerController 返回的图像超出图像边缘,添加黑条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52067802/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4