我有一个在自定义 UITableViewCell
中使用的图像。该图像是透明背景上的黑色三角形,我使用 CIColorInvert 滤镜使其成为透明背景上的白色三角形。
过滤后的(反转)图像正在垂直拉伸(stretch),如何阻止这种情况发生?
如果我直接加载UIImage
而不进行过滤,我得到的是这样的:
如果我应用CIFilter
我得到的是这样的:
这是我的代码:
// 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
中设置图像,如下所示:
cell.arrow.image = rightArrow;
UIImageView
尺寸为 15x15(以 IB 为单位)。 TriangleRight.png 为 15x15 像素,TriangleRight@2x.png 为 30x30 像素。
为了防止 CIFilter
拉伸(stretch)图像,我缺少什么?
更新:
发布问题 5 分钟后,我发现了问题:我正在使用自动布局,并且 UIImageView
没有明确的高度限制(只有宽度)。添加明确的高度约束解决了这个问题。但是,这仍然无法解释为什么直接使用 UIImage
有效(即使没有明确的高度限制)。
最佳答案
UIImageOrientation originalOrientation = self->imageview.image.imageOrientation;
imageview.image = [[UIImage alloc] initWithCGImage:imgRef scale:1.0 orientation:originalOrientation];
此外,使用原始方向应该可以修复它。
关于ios - CIImage 滤镜正在垂直拉伸(stretch)我的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16135216/