iOS带有边框的UIImage具有厚实的圆角

6

我尝试使用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,宽度为60的效果,看起来仍然有点粗:

输入图像描述


我的猜测是这是使用分数线宽的产物。它正在进行一些插值以给你所要求的结果。尝试使用1.0或2.0作为线宽,我敢打赌你会得到更好的结果。(说到这个,对于Retina设备,1.5(点)应该解析为3个像素。) - Duncan C
伟大的想法,但使用1.0线宽和60宽度仍然看起来很奇怪(附加到问题的图像)。尽管这确实有助于解决问题,但并不能完全修复。 - michaellindahl
我发现使用图层属性绘制圆角矩形比使用核心图形更好。不确定为什么。尝试在视图的图层上设置lineWidth、borderColor和borderWidth,看看是否能得到更好的结果。 - Duncan C
2个回答

5

对于在家跟进的读者们:

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

将代码修改如下,线宽为1。

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

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

enter image description here


肯定是正确的答案,剪切是导致问题的原因。 - Guy Moreillon

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接