我想将带有属性(即特定字体和大小)的文本直接转换为CIImage,即不先将其绘制到屏幕上,这样我就可以使用自定义CIFilter来动态更改文本的外观。我怎么能这样做?
答案 0 :(得分:3)
以下是NSAttributedString
到NSImage
的方法。我没有测试转换为CIImage
(最后一行),但它不应该太难:
let string = NSAttributedString(string: "Hello World!", attributes: [NSFontAttributeName: NSFont.labelFontOfSize(10)])
let image = NSImage(size: string.size())
image.lockFocus()
NSGraphicsContext.saveGraphicsState()
NSGraphicsContext.currentContext()!.shouldAntialias = true
string.drawAtPoint(NSZeroPoint)
NSGraphicsContext.restoreGraphicsState()
image.unlockFocus()
let ciimage = CIImage(data: image.TIFFRepresentation!)!