IOS UIImage with border has thick corner radius
I am trying to create a UIImage in code using CoreGraphics (via PaintCode) that has a border and corner radius. I find the image has a noticeably thicker border around the corner. It seems to be either an IOS bug or something that I am completely missing. Please advise.
code:
CGRect rect = CGRectMake(0, 0, 53, 100);
//// UIGraphics for UIImage context
CGContextRef context = UIGraphicsGetCurrentContext();
UIGraphicsPushContext(context);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
//// Rectangle Drawing
UIBezierPath *rectanglePath = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:cornerRadius];
[backgroundColor setFill];
[rectanglePath fill];
[borderColor setStroke];
rectanglePath.lineWidth = 1.4;
[rectanglePath stroke];
//// UIBezierPath to Image
CGContextAddPath(context, rectanglePath.CGPath);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsPopContext();
UIGraphicsEndImageContext();
return image;
Picture
Here's what line width 1 and width 60 looks like, it still seems a little thick:
source to share
For those of you following at home:
Thanks to the helpful @PixelCutCompany I need to insert borders for the UIBezierPath so that the border does not exceed the border of the image I am drawing.
Changed code as follows for line width 1.
CGRect boundedRectForRadius = CGRectMake(1, 1, 58, 98);
UIBezierPath *rectanglePath = [UIBezierPath bezierPathWithRoundedRect:boundedRectForRadius cornerRadius:cornerRadius];
Without that, he cropped the image like this:
source to share
The problem could also be in the coordinates of the actual pixel offset pixels. Please check this discussion for more information: Core Graphics does not draw a line with the correct width
source to share