iOS使用CGContext画线的质量比SpriteKit中的SKShapeNode差很多

iOS the quality of drawing lines using CGContext is much worse than SKShapeNode in SpriteKit

我正在制作一款用户可以用手指画线的游戏。网站上有很多方法。我尝试了两种方法,一种使用 UIView (UIKit) 中的 CGContext,另一种使用 SpriteKit 中的 CGPath 和 SKShapeNode。但后者显示出更好的质量。第一个使用 CGContext 的有丑陋的粗糙边缘。

请查看以下屏幕截图。我还在此处附上了这两种方法的部分代码(在 touchesMove 函数中)。

注:var ref = CGPathCreateMutable()

UIView 中的 CGContext

    CGPathAddLineToPoint(ref, nil, currentPoint.x, currentPoint.y)

    UIGraphicsBeginImageContext(self.frame.size)
    let context = UIGraphicsGetCurrentContext()
    tempImageView.image?.drawInRect(CGRect(x: 0, y: 0, width: self.frame.size.width, height: self.frame.size.height))


    CGContextAddPath(context, ref)

    // 3
    CGContextSetLineCap(context, CGLineCap.Round)
    CGContextSetLineWidth(context, brushWidth)
    CGContextSetRGBStrokeColor(context, red, green, blue, 1.0)
    CGContextSetBlendMode(context, CGBlendMode.Normal)

    // 4
    CGContextStrokePath(context)

    // 5
    UIGraphicsEndImageContext()

SpriteKit 中的 SKShapeNode 注:var lineDrawing = SKShapeNode()

CGPathAddLineToPoint(ref, nil, location.x, location.y)
lineDrawing.path = ref
lineDrawing.lineWidth = 3
lineDrawing.strokeColor = UIColor.blackColor()
lineDrawing.alpha = 1.0
self.addChild(lineDrawing)

如何在UIView中画出与SKShapeNode质量相同的线条?

一个明显的问题是您使用的是无法处理 Retina 屏幕的过时函数:

UIGraphicsBeginImageContext(self.frame.size)

你应该改用这个:

UIGraphicsBeginImageContextWithOptions(self.frame.size, false, 0)

如果设备有 Retina 屏幕,WithOptions 版本将 0 作为最后一个参数,以 Retina 分辨率创建图像上下文。 (0 参数表示“使用设备屏幕的比例因子”。)

可能还有其他问题,因为您没有显示在每个测试用例的路径中创建点的代码。