ios - 如何提高CGLayer的绘图质量

标签 ios uiview core-graphics uibezierpath cglayer

我使用 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 的代码时做错了什么?

Preview

最佳答案

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

layer.contentsScale = [UIScreen mainScreen].scale;

关于ios - 如何提高CGLayer的绘图质量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18570432/

相关文章:

ios - 在 UITableViewController 中将 UIView 放在 UITableView 前面

ios - map 的自定义注释 View

objective-c - 使用 CGAfficeTransform 移动 UILabel 并调整其大小

ios - 如何回到 View Controller ?

ios - 当应用程序进入前台时,如何重新启动基于 block 的动画?

iphone - OTA - 此时无法安装 myApp - 无法验证可执行文件

ios - 修改 UIView 扩展的所有子项一次

objective-c - 将 NSImage* 转换为 CGImageRef?

ios - 如何集成 InAppSettingsKit?

ios - Xcode 6 - 在 iPhone 4/5 上使用大小类