使用CIImage添加纯色边框

6
我正在寻找一种使用Core Image为现有图像添加纯色边框的方法。我已经找到了滤镜列表参考,但没有找到适合的滤镜。
帮帮我吧!
2个回答

2
我们需要获取CIImage范围或CGRect来创建实线边框。然后,我们可以在指定区域绘制形成实线的CIImage,并重复这些步骤3次,以便在不同位置绘制完整的实心矩形。以下是绘制指定区域上方直线的代码片段。
CIImage *overlay1 = [CIImage imageWithColor:[CIColor colorWithRed:255/255.f green:0/255.f blue:0/255.f alpha:1.00f]];
    overlay1 = [overlay1 imageByCroppingToRect:image.extent];
    overlay1 = [overlay1 imageByApplyingFilter:@"CIPerspectiveTransformWithExtent" withInputParameters:@{@"inputExtent":[CIVector vectorWithCGRect:image.extent],@"inputTopLeft":[CIVector vectorWithCGPoint:CGPointMake(topLeft.x - 5, topLeft.y + 5)],@"inputTopRight":[CIVector vectorWithCGPoint:CGPointMake(topRight.x + 5, topRight.y + 5)],@"inputBottomLeft":[CIVector vectorWithCGPoint:CGPointMake(topLeft.x - 5, topLeft.y )],@"inputBottomRight":[CIVector vectorWithCGPoint:CGPointMake(topRight.x + 5, topRight.y ) ]}];
    overlay = [ overlay1 imageByCompositingOverImage:overlay];

我已将宽度保持为5像素。topLeft,topRight...是相应位置的CGPoint。对于完整的矩形,您还需要bottomLeft和bottomRight。

覆盖是原始CIImage。


不确定为什么需要透视变换。为什么不只是将其裁剪成一条宽度为5的矩形,重复4次呢? - user1055568

1
这不完全是你所询问的内容,但如果你只想显示带边框的图像(而不是实际绘制边框),那么这可能更好...
你可以使用 CALayer 为任何 UIView 添加边框(以及圆角、阴影等)。
// imgView is an instance of UIImageView, but this works with any UIView
imgView.layer.borderWidth = 2.0f;
imgView.layer.borderColor = [[UIColor blackColor] CGColor];

你还需要 #import <QuartzCore/QuartzCore.h> 并链接到 QuartzCore 框架才能使其工作。


谢谢jhabbott的回复,但是这并没有帮到我。我正在尝试修改图像,而不仅仅是在顶部显示边框。 - frimoldi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接