5

我正在尝试使用具有边框和角半径的 CoreGraphics(通过 PaintCode)在代码中构建 UIImage。我发现图像的拐角处有明显较厚的边框。这似乎要么是 iOS 错误,要么是我完全遗漏的东西。请指教。

代码:

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;

图片:

较粗的圆角半径

下面是 lineWidth 为 1 和 width 为 60 的样子,看起来还是有点粗:

在此处输入图像描述

4

2 回答 2

5

对于那些在家里跟随的人:

感谢有帮助的@PixelCutCompany,我需要插入 UIBezierPath 的边界,以便边框不会超过我正在绘制的图像的框架。

对 lineWidth 为 1 的代码进行了如下修改。

CGRect boundedRectForRadius = CGRectMake(1, 1, 58, 98);
UIBezierPath *rectanglePath = [UIBezierPath bezierPathWithRoundedRect:boundedRectForRadius cornerRadius:cornerRadius];

没有这个,它会像这样裁剪图像:

在此处输入图像描述

于 2015-08-03T20:41:07.093 回答
0

问题也可能在于移动真实屏幕像素坐标。请查看此讨论以获取更多详细信息: Core Graphics is not draw a line at the correct width

于 2015-08-03T21:27:26.237 回答