使用 CIImage 添加纯色边框

问题描述 投票:0回答:3

我正在寻找一种使用 Core Image 将纯色边框添加到现有图像的方法。我找到了过滤器列表参考,但没有人制作它。

救命!!

iphone objective-c ios
3个回答
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。对于完整的矩形,您还需要左下角和右下角。

Overlay 是原始的 CIImage .


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 框架才能正常工作。


0
投票

这应该是最简单的解决方案。

CIImage *borderColorImage = [CIImage imageWithColor:[CIColor colorWithRed:RedColor green:GreenColor blue:BlueColor alpha:1.0]];
    borderColorImage = [borderColorImage imageByCroppingToRect:CGRectInset(image.extent, -SizeOfBorderX, -SizeOfBorderY)];
    CIImage *ImageWithBorder = [image imageByCompositingOverImage: borderColorImage];
© www.soinside.com 2019 - 2024. All rights reserved.