Вот откорректированный код, предоставленный создателями Macaw, который, кажется, решает проблему.Мне нужно было добавить инвертор, поскольку изображение рисовалось вверх ногами, и их первоначальное предложение NSGraphicsContext.current? .GraphicsPort не казалось стабильным / надежным, и я в итоге использовал NSGraphicsContext.current? .CgContext вместо:
func svgToNSImage(resourcePath: String, size: CGSize) -> NSImage? {
if let rootNode = try? SVGParser.parse(path: resourcePath) {
let macawView = MacawView(node: rootNode, frame: CGRect(origin: CGPoint.zero, size: size))
macawView.wantsLayer = true
let image = NSImage(size: macawView.bounds.size)
image.lockFocus()
// if let ctx = NSGraphicsContext.current?.graphicsPort {
if let ctx = NSGraphicsContext.current?.cgContext {
// image is drawing upside down, invert it and render
ctx.translateBy(x: 0, y: size.height)
ctx.scaleBy(x: 1.0, y: -1.0)
macawView.layer?.render(in: ctx)
}
image.unlockFocus()
return image
} else { return nil }
}