gpt4 book ai didi

ios - 有没有比 CGContext 更好的替代品?

转载 作者:可可西里 更新时间:2023-11-01 00:59:09 25 4
gpt4 key购买 nike

我正在使用 CGContext 创建一个漏斗形状,首先绘制一个三角形,然后绘制一条线。我正在我的 UIView 子类的 drawRect 中实现它,但想知道是否有另一种绘制方法,因为线条有点模糊。

    override func drawRect(rect: CGRect) {

let context: CGContextRef = UIGraphicsGetCurrentContext()!
CGContextClearRect(context, rect);

let rectWidth: CGFloat = 15

// Line
CGContextSetRGBStrokeColor(context, 0, 0, 0, 1)
CGContextMoveToPoint(context, rectWidth / 2, 5)
CGContextAddLineToPoint(context, rectWidth / 2, 10)
CGContextSetStrokeColorWithColor(context, UIColor.blueColor().CGColor)
CGContextSetLineCap(context, CGLineCap.Round)
CGContextSetLineWidth(context, 3.0)
CGContextStrokePath(context)

// Triangle
CGContextBeginPath(context);
CGContextMoveToPoint (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
CGContextAddLineToPoint(context, CGRectGetMidX(rect), 8);
CGContextAddLineToPoint(context, CGRectGetMaxX(rect), CGRectGetMinY(rect));
CGContextClosePath(context);

UIColor.blueColor().setFill()
CGContextFillPath(context);

}

产生这个: enter image description here

但是当我导入我设计的图像时,图像更清晰(您可以在 iPhone 上看出来): enter image description here

那么,有没有更好的方法来实现这个输出呢? CGContext 行有点模糊,所以我想知道是否有更好的选择以及这些方法的优缺点。

最佳答案

Core Graphics 正在绘制模糊的东西,这可能是因为您告诉它这样做。 CG 坐标系中每对整数坐标的位置恰好在网格线上——如果您在这些坐标之间绘制一个点宽的笔划,您将得到一个横跨网格线两边半个点的笔划。根据您正在绘制的位图上下文的比例,这可能会导致两列半阴影像素而不是一列全阴影像素 - 即模糊线。

在你的情况下,你画线的一些点落在全像素边界上,一些点落在半像素边界上,所以在 1x 显示器上,一些线会模糊,而在 2x 或 3x 显示器上,其他线会模糊是。了解 Points versus Pixels在 Apple 的绘图文档中,您可能会找到一种方法来偏移坐标,使线条都落在您想要的位置。

关于ios - 有没有比 CGContext 更好的替代品?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38236255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com