Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add solid color border with CIImage

I'm looking for a way to add a solid color border to an existent image with Core Image. I've found the filter list reference but there is no one to make it.

Help !!

like image 596
frimoldi Avatar asked Jul 01 '12 00:07

frimoldi


2 Answers

We need to have the CIImage extent or the CGRect in which we want to create the solid border. Than, We can draw a CIImage forming a solid line in the specified area, and repeat the steps for 3 more times for different positions to draw a complete solid rectangle. Following is the piece of code which will draw a straight solid line above the specified area.

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];

I have kept the width for 5 pixels. topLeft , topRight.... are the respective CGPoint for the position. For a complete rectangle you will also need bottomLeft and bottomRight.

Overlay is the original CIImage .

like image 183
Vidit Avatar answered Nov 05 '22 05:11

Vidit


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.

like image 22
jhabbott Avatar answered Nov 05 '22 07:11

jhabbott