菜鸟教程小白 发表于 2022-12-11 20:30:05

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


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

<p>我的代码:</p>

<pre><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: ) {
    if let image = info as? UIImage {
      testImageView.image = image
    }
    picker.dismiss(animated: true)
}
</code></pre>

<p>结果如下:</p>

<p> <a href="/image/PgYJ7.jpg" rel="noreferrer noopener nofollow"><img src="/image/PgYJ7.jpg" alt="enter image description here"/></a> </p>

<p>注意我已经尝试将 ImageView 的 <code>contentMode</code> 更改为缩放以填充以及方面填充(不适合)。我不知道为什么将黑条添加为图像本身的一部分。有人可以帮忙吗?
<a href="/image/wsrIY.jpg" rel="noreferrer noopener nofollow"><img src="/image/wsrIY.jpg" alt="enter image description here"/></a> </p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>如果裁剪后的图像在原点之外,我已经通过使用原始图像解决了这个问题</p>

<pre><code>guard let selectedImage = info[.editedImage] as? UIImage else {
      fatalError(&#34;Expected a dictionary containing an image, but was provided the following: \(info)&#34;)
    }

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

}
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 从 UIImagePickerController 返回的图像超出图像边缘,添加黑条,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/52067802/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/52067802/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 从 UIImagePickerController 返回的图像超出图像边缘,添加黑条