我让用户从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)
}
结果如下:
请注意,我已经尝试过更改图像视图的contentMode
的比例以进行填充和填充(不适合)。我不知道为什么将黑条添加为图像本身的一部分。有人可以协助吗?
答案 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
}
}