【问题标题】:How to Convert Text With Attributes Directly Into a CIImage (Without Drawing to Screen)如何将带有属性的文本直接转换为 CIImage(无需绘制到屏幕)
【发布时间】:2016-12-05 08:22:56
【问题描述】:

我想将具有属性(即特定字体和大小)的文本直接转换为 CIImage(即,无需先将其绘制到屏幕上),以便我可以使用自定义 CIFilter 来动态更改文本的外观。我该怎么做?

【问题讨论】:

    标签: swift macos cocoa cifilter ciimage


    【解决方案1】:

    以下是如何将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!)!
    

    【讨论】:

    • 谢谢!那成功了。此外,转换为 CIImage 工作正常,我的自定义 CIFilter 处理它没有问题。
    猜你喜欢
    • 2018-08-09
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多