首页 文章

如何将NSImage绘制到CGContext

提问于
浏览
0

我有一个对象NSImage,我想绘制到CGContext . 我发现CGContext没有相应的方法,但似乎有一个CGImage方法,剪辑

func clip(to: CGRect, mask: CGImage)

请问我该怎么做?

1 回答

  • 0

    最简单的方法是创建 NSGraphicsContext 包装你的 CGContext ,并绘制到:

    let gc: CGContext = ...
    let nsImage: NSImage = ...
    let rect: CGRect = ...
    
    let priorNsgc = NSGraphicsContext.current
    defer { NSGraphicsContext.current = priorNsgc }
    NSGraphicsContext.current = NSGraphicsContext(cgContext: gc, flipped: false)
    nsImage.draw(in: rect)
    

相关问题