Swift 从Cocoa中的CGContext保存路径
我正在尝试使用Cocoa为Swift中的macOS编写一个小型绘图应用程序。问题在于,当再次绘制Swift 从Cocoa中的CGContext保存路径,swift,macos,cocoa,core-graphics,Swift,Macos,Cocoa,Core Graphics,我正在尝试使用Cocoa为Swift中的macOS编写一个小型绘图应用程序。问题在于,当再次绘制NSView时,最后绘制的线将消失。有没有办法保持画好的线?我可以将点保存在数组中,但这会大大降低性能。不幸的是,CGContextSaveGState函数不保存路径 这是我正在绘制的NSView的课程(使用鼠标或手写笔): 对于这个场景,我会跳出CoreGraphics,使用NSBezierPath。您可以将其保存在属性中。然后,当更多点进入时,您可以调用lineToPoint,然后调用stroke
NSView
时,最后绘制的线将消失。有没有办法保持画好的线?我可以将点保存在数组中,但这会大大降低性能。不幸的是,CGContextSaveGState
函数不保存路径
这是我正在绘制的NSView
的课程(使用鼠标或手写笔):
对于这个场景,我会跳出CoreGraphics,使用
NSBezierPath
。您可以将其保存在属性中。然后,当更多点进入时,您可以调用lineToPoint
,然后调用stroke
来绘制它。例如:
let strokeColor = NSColor(red: red, green: green, blue: blue, alpha: 1.0)
let path = NSBezierPath()
path.lineWidth = brushSize
path.lineCapStyle = .RoundLineCapStyle
path.lineJoinStyle = .RoundLineJoinStyle
path.moveToPoint(lastPoint)
path.lineToPoint(currentPoint)
...
strokeColor.setStroke()
path.stroke()
您正在查找CGContextCopyPath。它给你一个CGPath伪对象。保持这一点的最简单方法是将NSBezierPath作为其
路径
填充到NSBezierPath中,因为NSBezierPath是一个真实的对象,ARC将为您进行内存管理
let strokeColor = NSColor(red: red, green: green, blue: blue, alpha: 1.0)
let path = NSBezierPath()
path.lineWidth = brushSize
path.lineCapStyle = .RoundLineCapStyle
path.lineJoinStyle = .RoundLineJoinStyle
path.moveToPoint(lastPoint)
path.lineToPoint(currentPoint)
...
strokeColor.setStroke()
path.stroke()