【问题标题】:How to improve drawing quality of CGLayer如何提高CGLayer的绘图质量
【发布时间】:2013-09-02 09:59:33
【问题描述】:

我使用 CoreGraphics 在自定义 SGCircleView 中绘制圆圈。因为我希望在触摸时用径向渐变填充圆,所以我的方法是用原始圆(SGCircleLayer)绘制一层,然后用渐变添加另一层。问题是这些圆圈看起来“有颗粒感”。

这是SGCircleLayer中的代码:

- (void) drawLayer: (CALayer *) layer inContext: (CGContextRef) ctx {
    UIGraphicsPushContext(ctx);
    UIBezierPath *circlePath = [UIBezierPath bezierPathWithOvalInRect: CGRectInset(layer.bounds, CLineWidth / 2, CLineWidth / 2)];
    self.path = circlePath.CGPath;
    [[UIColor whiteColor] setStroke];
    circlePath.lineWidth = CLineWidth;
    [circlePath stroke];
    UIGraphicsPopContext();
}

出于测试目的,我在自定义 SGCircleTestView 的 drawRect 中添加了另一个半径和线宽完全相同的圆,看起来真的很平滑。

这是我在 SGCircleTestView 中的 drawRect 的代码(平滑圆):

- (void) drawRect: (CGRect) rect {
    CGRect bounds = CGRectInset(rect, CLineWidth * 0.5, CLineWidth * 0.5);
    UIBezierPath* circlePath = [UIBezierPath bezierPathWithOvalInRect: bounds];
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, CLineWidth2);
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    [circlePath stroke];
}

我在绘制 SGCircleLayer 的代码时做错了什么?

【问题讨论】:

    标签: ios uiview core-graphics uibezierpath cglayer


    【解决方案1】:

    您可能需要将图层的 contentsScale 设置为与屏幕相同。

    layer.contentsScale = [UIScreen mainScreen].scale;
    

    【讨论】:

      【解决方案2】:

      【讨论】:

        猜你喜欢
        • 2021-07-24
        • 1970-01-01
        • 1970-01-01
        • 2023-04-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-01-30
        • 2020-04-21
        相关资源
        最近更新 更多