CIImage过滤器垂直拉伸我的图像
我有一个我在自定义 UITableViewCell
中使用的图片。图像是透明背景上的黑色三角形,我使用 CIColorInvert
过滤器,使其成为透明背景上的白色三角形。
I have an image which I'm using in a custom UITableViewCell
. The image is a black triangle on a transparent background and I'm using a CIColorInvert
filter to make it a white triangle on a transparent background.
过滤的(倒置的)图像被垂直拉伸,如何阻止这种情况发生?
The filtered (inverted) image is being stretched vertically, how can I stop this from happening?
如果加载 UIImage
直接不过滤我得到的是:
If I load the UIImage
directly without doing the filtering what I get is this:
如果我应用 CIFilter
我得到的是:
If I apply the CIFilter
what I get is this:
这是我的代码:
// create the white triangle
CIImage *inputImage = [[CIImage alloc] initWithImage:
[UIImage imageNamed:@"TriangleRight"]];
CIFilter *invertFilter = [CIFilter filterWithName:@"CIColorInvert"];
[invertFilter setDefaults];
[invertFilter setValue: inputImage forKey: @"inputImage"];
CIImage *outputImage = [invertFilter valueForKey: @"outputImage"];
CIContext *context = [CIContext contextWithOptions:nil];
rightArrow = [UIImage imageWithCGImage:[context createCGImage:outputImage fromRect:outputImage.extent]];
在这两种情况下,我设置的图像在 UIImageView
如下:
In both cases I'm setting the image in the UIImageView
as follows:
cell.arrow.image = rightArrow;
UIImageView
尺寸为15x15 )。 TriangleRight.png是15x15像素,TriangleRight@2x.png是30x30像素。
The UIImageView
dimensions are 15x15 (in IB). TriangleRight.png is 15x15 pixels, TriangleRight@2x.png is 30x30 pixels.
我缺少什么来防止 CIFilter
从拉伸图像?
What am I missing to prevent the CIFilter
from stretching the image?
更新:
发布问题后5分钟我想出了问题:使用自动布局和 UIImageView
没有显式高度约束(只有宽度)。添加显式高度约束解决了问题。但是,仍然没有解释为什么使用 UIImage
直接工作(即使没有显式的高度约束)。
UPDATE:
5 minutes after posting the question I figured out the problem: I'm using Auto Layout and the UIImageView
didn't have an explicit height constraint (only width). Adding an explicit height constraint solved the problem. However, that still doesn't explain why using the UIImage
directly worked (even without an explicit height constraint).
UIImageOrientation originalOrientation = self->imageview.image.imageOrientation;
imageview.image = [[UIImage alloc] initWithCGImage:imgRef scale:1.0 orientation:originalOrientation];
此外,使用原来的方向应该已经固定。
Also, using the original orientation should have fixed it.