在 Swift 中填充未闭合的贝塞尔曲线路径
Fill an unclosed bezier path in Swift
我正在尝试创建一张与左侧相似的图片。我有一个 NSBezier 路径(不是封闭路径)但是当我填充它时,它似乎只产生右边的图像。我想填充路径,但只填充环状部分。有什么建议吗?
UIGraphicsBeginImageContext(board.size)
if let context = UIGraphicsGetCurrentContext() {
if let path = snake.path {
context.setShouldAntialias(false)
let transformedPath = CGMutablePath()
transformedPath.addPath(path, transform: transform)
context.addPath(transformedPath)
context.setLineJoin(.round)
context.setLineCap(.round)
context.setLineWidth(snake.thickness)
context.setStrokeColor(UIColor.magenta.cgColor)
context.strokePath()
context.fillPath()
}
if let newImage = UIGraphicsGetImageFromCurrentImageContext()?.cgImage {
board.texture = SKTexture(cgImage: newImage)
}
}
UIGraphicsEndImageContext()
您没有阅读 strokePath
documentation,其中说:
The current path is cleared as a side effect of calling this function.
所以当您调用 fillPath
时,当前路径是空的。没什么可填的。在调用 fillPath
.
之前,您需要再次调用 context.addPath(transformedPath)
来再次设置路径
如果你想先填充再描边,你可以调用context.drawPath(using: .fillStroke)
。此方法填充,然后描边当前路径(然后清除当前路径)。
此外,您没有设置上下文的填充颜色。默认填充颜色为黑色,这可能不是您想要的颜色。
我正在尝试创建一张与左侧相似的图片。我有一个 NSBezier 路径(不是封闭路径)但是当我填充它时,它似乎只产生右边的图像。我想填充路径,但只填充环状部分。有什么建议吗?
UIGraphicsBeginImageContext(board.size)
if let context = UIGraphicsGetCurrentContext() {
if let path = snake.path {
context.setShouldAntialias(false)
let transformedPath = CGMutablePath()
transformedPath.addPath(path, transform: transform)
context.addPath(transformedPath)
context.setLineJoin(.round)
context.setLineCap(.round)
context.setLineWidth(snake.thickness)
context.setStrokeColor(UIColor.magenta.cgColor)
context.strokePath()
context.fillPath()
}
if let newImage = UIGraphicsGetImageFromCurrentImageContext()?.cgImage {
board.texture = SKTexture(cgImage: newImage)
}
}
UIGraphicsEndImageContext()
您没有阅读 strokePath
documentation,其中说:
The current path is cleared as a side effect of calling this function.
所以当您调用 fillPath
时,当前路径是空的。没什么可填的。在调用 fillPath
.
context.addPath(transformedPath)
来再次设置路径
如果你想先填充再描边,你可以调用context.drawPath(using: .fillStroke)
。此方法填充,然后描边当前路径(然后清除当前路径)。
此外,您没有设置上下文的填充颜色。默认填充颜色为黑色,这可能不是您想要的颜色。