关于swift: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
enter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    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
enter
注意:var lineDrawing = SKShapeNode()

1
2
3
4
5
6
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 屏幕的过时函数:

1
UIGraphicsBeginImageContext(self.frame.size)

你应该改用这个:

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

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

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

  • 它更好,但仍然不如 SKShapeNode。创建路径的代码如下所示: (1)var ref = CGPathCreateMutable(); (2)在touchesBegan中,CGPathMoveToPoint(ref, nil, location.x, location.y); (3) 在 touchesMove 中,CGPathAddLineToPoint(ref, nil, location.x, location.y)。我错过了什么吗?
  • 位置是 let location = touch.locationInView(self)
  • 尝试在两次测试中以相同的速度绘制相同的形状(用手指或其他方式)。 CGContext 实现中路径的最终点数是多少? SpriteKit 版本中有多少个?
  • 不知道为什么我把这个设置为 1.0。只是改进了我自己的游戏很多。
  • @robmayoff 我认为你是对的。我有多个 UIGraphicsBeginImageContext 行,我在其中进行屏幕截图并转换为图像并分析像素信息。只换一行是行不通的,但是当我全部更改时它会起作用。但是,双倍像素会耗尽我的 CPU,因为我正在进行大量像素编辑。所以我最终不得不坚持过时的功能并添加 self.layer.shouldRasterize = true 以使其更好,尽管不如视网膜功能。
  • @robmayoff 但我仍然不明白为什么在视网膜/非视网膜功能中画线会导致 CPU 产生如此大的差异。比如游戏本身消耗了~15%的CPU,当我使用第一个函数添加线时,CPU的变化可以忽略不计,但是当视网膜使用第二个函数时,CPU上升到60%,即使只有一个屏幕上的视网膜线。你对我有什么建议吗?如果有帮助,我可以在这里放更多代码。
  • @DanielKanaan 设置为 1.0 和 0 给我同样的结果。但是视网膜功能比非视网膜功能消耗更多的CPU,我不知道为什么。我只画线并将它们截屏为图像,然后呈现在 UIImageView 中。


来源:https://www.codenong.com/37981104/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_10020.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?