我想创建一个基于矢量的NSImage,所以当重绘任何尺寸时,它将很平滑。我可以阅读一个eps文件并绘制此文件,它非常平滑。放大时没有锯齿状的边缘。
我有这段代码,但是它会生成一个bipmap图像,而不是矢量图像。
class Point {
let x: Double
let y: Double
init(x: Double, y: Double) {
self.x = x
self.y = y
}
}
var points = [Point]()
var destinations = [Int]()
func toNSImage() -> NSImage {
let size = NSSize(width: width, height: height)
let image = NSImage(size: size)
image.lockFocusFlipped(true)
let ctx = NSGraphicsContext.current!.cgContext
if points.count > 0 {
ctx.move(to: CGPoint(x: points[0].x, y: points[0].y))
var idx = 0
//print(destinations.count)
for _ in (0...destinations.count - 1) {
idx = destinations[idx]
ctx.addLine(to: CGPoint(x: points[idx].x, y: points[idx].y))
print(idx)
}
ctx.setLineWidth(CGFloat(0.5))
ctx.strokePath()
}
image.unlockFocus()
return image
}