6

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

帮助 !!

4

2 回答 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。

Overlay 是原始的 CIImage 。

于 2015-09-23T05:01:32.133 回答
1

This isn't exactly what you asked about, but it might be better if you just want to display the image with a border (rather than actually drawing a border onto it)...

You can use CALayer to add borders (and rounded corners, shadows, etc.) to any UIView...

// imgView is an instance of UIImageView, but this works with any UIView
imgView.layer.borderWidth = 2.0f;
imgView.layer.borderColor = [[UIColor blackColor] CGColor];

You also need to #import <QuartzCore/QuartzCore.h> and link to the QuartzCore framework for this to work.

于 2012-07-01T00:57:07.680 回答