简体   繁体   English

UIImageView框架是正确的,contentMode是正确的,图像无法正确显示

[英]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, 我检查了值,

  • the frame of all views are correct (300 * 400), 所有视图的框架都是正确的(300 * 400),
  • the size of the image is roughly the same (301* 401), 图片的大小大致相同(301 * 401),
  • the scale and inset of the image is correct 图像的比例和插图正确

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.

相关问题 在将contentMode设置为UIViewContentModeScaleAspectFit时,如何设置UIImageView左对齐或右对齐? - how to set the UIImageView align left or right when set the contentMode to UIViewContentModeScaleAspectFit? UICollectionViewCell UIImageView 忽略 contentMode,溢出图像 - UICollectionViewCell UIImageView ignores contentMode, overflows image 如何在右侧的UIButton上添加图像,而contentMode不适用于图像? - How to add Image on UIButton on right side and contentMode don't working with image? UIImageView从左到右查看图像 - UIImageView view Image from Left to Right 在UIImageView中水平对齐UIButton的图像 - Align Image of UIButton horizontally right in UIImageView 在ViewController中实例化UIImageView时如何设置其大小/框架和contentMode? - How to set size/frame and contentMode of a UIImageView when its instantiated in the viewController? 将点从图像转换为UIImageView点,contentMode-aware - Convert points from image to UIImageView points, contentMode-aware 在运行时设置图像时不遵守UIImageView的contentmode - UIImageView's contentmode not respected when setting image during runtime UIImageView contentMode Scale Aspect Fill 正在拉伸图像(没关系,它不是) - UIImageView contentMode Scale Aspect Fill is Stretching the Image (Nevermind, It Wasn't) UIImageView contentMode页首 - UIImageView contentMode Top
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM