如何将NSImage绘制到CGContext [英] How to draw NSImage to CGContext

查看:70
本文介绍了如何将NSImage绘制到CGContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an object NSImage, which I want to draw to CGContext. I found that there is no corresponding method for CGContext, but there seems to be a CGImage method, clip

func clip(to: CGRect, mask: CGImage)

请问我该怎么做?

推荐答案

最简单的方法是创建一个NSGraphicsContext包装您的CGContext并绘制到其中:

The easiest way is to create an NSGraphicsContext wrapping your CGContext, and draw into that:

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)

这篇关于如何将NSImage绘制到CGContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆