将图像转换为CGImage时失去图像方向 [英] Losing image orientation while converting an image to CGImage

查看:197
本文介绍了将图像转换为CGImage时失去图像方向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从矩形原始图像中裁剪图像的正方形部分时,我面临图像方向问题.当图像处于横向时,就可以了.但是当它为纵向时,似乎无法保留图像方向,这会导致图像方向错误且裁剪不佳:

I'm facing an image orientation issue when cropping a square portion of an image out of a rectangular original image. When image is in landscape, it's fine. But when it is in portrait, it seems that the image orientation is not preserved, which result in an image with wrong orientation AND bad crop:

 func cropImage(cropRectangleCoordinates: CGRect) {

        let croppedImage = originalImage

        let finalCroppedImage : CGImageRef = CGImageCreateWithImageInRect(croppedImage.CGImage, cropRectangleCoordinates)

        finalImage =  UIImage(CGImage: finalCroppedImage)!


    }

我认为问题出在croppedImage.CGImage.此处图像被转换为​​CGImage,但似乎没有保留方向. 仅通过使用UIImage即可保持方向容易,但是要进行裁剪,图像需要暂时为CGImage,这就是问题所在.即使我在转换回UIImage时重新定向了图像,它的方向也可能正确,但是裁剪CGImage时已经损坏了.

I think the problem is with croppedImage.CGImage. Here the image gets converted to CGImage, but it seems not to preserve the orientation. It's easy to preserve orientation by using UIImage only, but to make the crop, image needs to be temporarily CGImage and this is the problem. Even if I reorient the image when converting back to UIImage, it might be in the correct orientation but the damage is already done when cropping CGImage.

这是一个快速的问题,因此请尽快回答,因为在Objective-C中解决方案可能会有所不同.

This is a swift question, so please answer in swift, as the solution can be different in Objective-C.

推荐答案

我找到了一个解决方案....时间会证明它是否足够健壮,但它似乎在所有情况下都有效.那是一个恶毒的错误.

I found a solution.... time will tell if it's robust enough, but it seems to work in all situations. That was a vicious bug to fix.

所以问题是UIImage仅在某些情况下会在转换为CGImage时失去其方向.它会影响人像图像,并自动将其置于横向模式.因此,默认情况下为横向的图像不会受到影响. 但是,该错误的恶性之处在于它不会影响所有人像图像!同样,imageorientation值对某些图像也无济于事. 这些有问题的图像是用户在库中拥有的图像,这些图像是他从电子邮件,消息或从网络保存的,因此不是用相机拍摄的.这些图像可能没有方向信息,因此在某些情况下,是纵向的图像....转换为CGImage时的纵向肖像.直到我意识到自己的设备库中的某些图像是从消息或电子邮件中保存下来的,我才真正陷入困境.

So the problem is that UIImage, in some case only, lose it's orientation when converted to CGImage. It affects portraits image, that are automatically put in landscape mode. So image that are landscape by default are not affected. But where the bug is vicious is that it doesn't affect ALL portrait images !! Also imageorientation value won't help for some image. Those problematic images are images that user has in it's library that he got from email, messages, or saved from the web, so not taken with a camera. These images possibly don't have orientation information, and thus in some case, an image in portrait.... REMAINS in portrait when converted to CGImage. I really got stuck on that until I realized that some of my image in my device library were saved from messages or emails.

因此,我发现猜测哪个图像将被重定向的唯一可靠方法是创建给定图像的两个版本:UIImage和CGImage,并比较它们的高度值.如果它们相等,则CGImage版本将不会旋转,您可以按预期使用它. 但是,如果它们的高度不同,则可以确定CGImageCreateWithImageInRect的CGImage转换将使图像美化. 仅在这种情况下,我交换原点的x/y坐标,将其作为矩形坐标传递给裁剪对象,它正确地处理了这些特殊图像.

So the only reliable way I found to guess which image will be reoriented, is to create both version of a given image: UIImage, and CGImage, and compare their height value. If they are equal, then the CGImage version will not be rotated and you could work with it as expected. But if they height are different, you can be sure that the CGImage conversion from CGImageCreateWithImageInRect will landscapize the image. In this case only, I swap the x/y coordinate of origin, that I pass as rectangle coordinate to crop and it treats those special images correctly.

这是一篇很长的文章,但是主要思想是将CGImage高度与UIImage宽度进行比较,如果它们不同,则希望原点反转.

That was a long post, but the main idea is to compare CGImage height to UIImage width, and if they are different, expect origin point to be inverted.

这篇关于将图像转换为CGImage时失去图像方向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆