gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:01:54 24 4
gpt4 key购买 nike

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

最佳答案

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com