Is the width of the Core Graphics barcode incompatible between lines and arcs?

Use case: I will subclass UIView to create a custom view that "mattes" UIImage with a rounded rectangle (fixed the image with a rounded rectangle). The code works; I used a method similar to this question .

However, I want to stroke the clipping path to create a β€œframe”. This works, but the strokes of the arc look noticeably different from the strokes. I tried adjusting the stroke width to large values ​​(at first I thought it was pixelation), but anti-aliasing seems to handle arcs and lines differently.

Here is what I see on the simulator:

Uneven stroke image

This is the code that draws it:

CGContextSetRGBStrokeColor(context, 0, 0, 0, STROKE_OPACITY); CGContextSetLineWidth(context, 2.0f); CGContextAddPath(context, roundRectPath); CGContextStrokePath(context); 

Does anyone know how to make these lines smooth?

+6
ios core-graphics macos
source share
2 answers

... but anti-aliasing seems to handle arcs and lines differently.

No, it is not.

Your stroke width is consistent - it is 2 pt full.

It is wrong that you are attached to a rectangle, and your sides of the form are located directly above the edges of this rectangle, so only half the sides that are inside the rectangle are obtained. Therefore, the edges appear only 1 px wide.

The solution is not clamped to increase your cut-off rectangle by 2 pt on each axis until it is trimmed or to move the edges of your shape inward by 1 pt on each side. (ETA: Or, yes, take an inner punch.)

+12
source share

Just in case, someone is trying to do the same thing as me (round the image):

The UIImageView class has a layer property of type CALayer . CALayer already has built-in functionality (it was a bit surprising for me, I could not find it anywhere):

 UIImageView *thumbnailView = [UIImage imageNamed:@"foo.png"]; thumbnailView.layer.masksToBounds = YES; thumbnailView.layer.cornerRadius = 15.0f; thumbnailView.layer.borderWidth = 2.0f; [self.view addSubview:thumbnailView]; 

Also does the trick.

+2
source share

All Articles