ios - CIImage 滤镜正在垂直拉伸(stretch)我的图像

标签 ios cocoa-touch ios6 uiimageview core-image

我有一个在自定义 UITableViewCell 中使用的图像。该图像是透明背景上的黑色三角形,我使用 CIColorInvert 滤镜使其成为透明背景上的白色三角形。

过滤后的(反转)图像正在垂直拉伸(stretch),如何阻止这种情况发生?

如果我直接加载UIImage而不进行过滤,我得到的是这样的:

arrow ok, but black

如果我应用CIFilter我得到的是这样的:

enter image description here

这是我的代码:

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

相关文章:

iphone - 更改 UIImage 上触摸部分的 Alpha 值?

iphone - 如何限制应用程序只能在 iPhone 5 上运行?

iphone - 查找最小/最大纬度和经度

javascript - 在 ios 6 上隐藏 safari 的地址栏

ios - Swift:带有居中按钮的自定义导航栏

IOS CNContactStore executeSaveRequest 失败,CNErrorDomain Code=500

ios - 向导航栏添加另一个按钮

ios - 如何在GMSMapView中获取动画折线路线,使其在 map 移动时随 map 一起移动?

objective-c - 关于在版本控制下阻止项目的警告 (Xcode 4)

ios - UITableView 不使用 UISearchBar 滚动