Add a solid color border with CIImage

I am looking for a way to add a solid border of color to an existing image using Core Image. I found a filter list link, but no one can do it.

Reference!

+6
source share
2 answers

We need to have a degree of CIImage or CGRect in which we want to create a solid border. Then we can draw the CIImage, forming a solid line in the specified area, and repeat the steps three more times for different positions to draw a full solid rectangle. Below is a snippet of code that 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 kept the width for 5 pixels. topLeft, topRight .... are the appropriate CGPoint for the position. For a full rectangle, you'll also need bottomLeft and bottomRight.

Overlay is the original CIImage.

+2
source

This is not quite what you asked for, but it might be better if you just want to display the image using a border (rather than drawing a border on 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 #import <QuartzCore/QuartzCore.h> and a link to the QuartzCore structure for this.

+1
source

Source: https://habr.com/ru/post/919404/


All Articles