CIImage filter is stretching my image vertically

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-14 00:42:38

问题


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?

If I load the UIImage directly without doing the filtering what I get is this:

If I apply the CIFilter what I get is this:

Here's my code:

// 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]];

In both cases I'm setting the image in the UIImageView as follows:

cell.arrow.image = rightArrow;

The UIImageView dimensions are 15x15 (in IB). TriangleRight.png is 15x15 pixels, TriangleRight@2x.png is 30x30 pixels.

What am I missing to prevent the CIFilter from stretching the image?

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


回答1:


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.



来源:https://stackoverflow.com/questions/16135216/ciimage-filter-is-stretching-my-image-vertically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!