UIImageView框架是正确的,contentMode是正确的,图像没有正确显示

时间:2017-06-14 10:02:36

标签: ios swift core-data uiimageview

我从照片中选择一张图片:

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
    })
}

然后裁剪此图片:

@IBAction func crop(_ sender: UIButton) {
    let croppedCGImage = imageView.image?.cgImage?.cropping(to: cropArea)
    let croppedImage = UIImage(cgImage: croppedCGImage!)
    delegate.processCroppedImage(image: croppedImage, cropper: self)
}

然后将数据传递给集合视图单元格:

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)
}

当单元格获取数据时,它会执行以下操作:

if let imageData = knowledgeComponent?.image {
        if let image = UIImage(data: imageData as Data) {
            buttonStack.isHidden = true
            imageView.isHidden = false
            imageView.image = image
        }
    }

但是图片没有正确显示,它已放大。

我检查了数值,

  • 所有视图的框架都是正确的(300 * 400),
  • 图像的大小大致相同(301 * 401),
  • 图像的比例和插图正确

我已经查看了我能找到的所有问题,但他们都没有解决我的问题,我怀疑我的图像数据有问题,是不是这样?如果没有,那可能是什么?

这个错误使我不想再使用UIImageView :(

1 个答案:

答案 0 :(得分:0)

我认为我找到了问题(有点),感谢@ TawaNicolas的提示,图像裁剪器有问题,即使图像尺寸合适,也许在iPad上尺寸为300 * 400的图像不相等到尺寸为300 * 400的imageView?

无论如何,似乎我可以通过调整图像裁剪来改变结果,万岁!