[英]UIImageView frame is right, contentMode is right, image not displayed correctly
I pick an image from photos: 我从照片中选择一个图像:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let imageCropper = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "ImageCropperVC") as! ImageCropperViewController
imageCropper.delegate = self
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
picker.dismiss(animated: true, completion: nil)
present(imageCropper, animated: false, completion: {
imageCropper.imageView.image = image
})
}
Then crop this image: 然后裁剪此图像:
@IBAction func crop(_ sender: UIButton) {
let croppedCGImage = imageView.image?.cgImage?.cropping(to: cropArea)
let croppedImage = UIImage(cgImage: croppedCGImage!)
delegate.processCroppedImage(image: croppedImage, cropper: self)
}
Then pass the data a collection view cell: 然后将数据传递给集合视图单元格:
func processCroppedImage(image: UIImage, cropper: ImageCropperViewController) {
let imageData = UIImagePNGRepresentation(image)! as NSData
let i = Side(context: managedObjectContext)
i.image = imageData
imagePickerTarget!.knowledgeComponent = i
cropper.dismiss(animated: true, completion: nil)
}
When the cell gets the data, it does this: 当单元获取数据时,它将执行以下操作:
if let imageData = knowledgeComponent?.image {
if let image = UIImage(data: imageData as Data) {
buttonStack.isHidden = true
imageView.isHidden = false
imageView.image = image
}
}
But the image isn't displayed correctly, it's zoomed in. 但是图像显示不正确,它被放大了。
I checked the values, 我检查了值,
I've look every question I can find but all of them don't solve my problem, I suspect there's something wrong with my image data, could it be the case? 我看过我能找到的每个问题,但所有问题都不能解决我的问题,我怀疑图像数据有问题,是这样吗? If not, what could be? 如果没有,那可能是什么?
This bug makes me not want to use UIImageView
anymore :( 这个错误使我不想再使用UIImageView
了:(
I think I found the problem (sort of), thanks to @TawaNicolas's hint, there's something wrong with the image cropper, even though the image size is right, maybe on the iPad an image sized 300*400 doesn't equal to an imageView with a size 300*400? 我想我发现了问题(某种程度上),这要归功于@TawaNicolas的提示,即使图像尺寸合适,图像裁剪器还是存在问题,也许在iPad上,图像尺寸为300 * 400的图像不等于imageView尺寸为300 * 400?
Anyway, it seems like I'm able to change the outcome by tuning the image cropper, hooray! 无论如何,似乎我可以通过调整图像裁剪器来改变结果,万岁!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.