使用CIImage添加纯色边框

我正在寻找一种方法,使用Core Image为现有图像添加纯色边框。 我找到了filter列表参考,但没有人可以做到。

帮帮我 !!

我们需要有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。

这不完全是你所问的,但如果你只是想用边框显示图像(而不是实际在边框上绘制边框)可能会更好……

您可以使用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框架以实现此function。