0
我有一个NSAttributedString,我想绘制成一个CGImage,以便以后可以将CGImage绘制成一个NSView。这是我到目前为止有:如何从NSAttributedString创建剪贴蒙版?
// Draw attributed string into NSImage
NSImage* cacheImage = [[NSImage alloc] initWithSize:NSMakeSize(w, h)];
[cacheImage lockFocus];
[attributedString drawWithRect:NSMakeRect(0, 0, width, height) options:0];
[cacheImage unlockFocus];
// Convert NSImage to CGImageRef
CGImageSourceRef source = CGImageSourceCreateWithData(
(CFDataRef)[cacheImage TIFFRepresentation], NULL);
CGImageRef img = CGImageSourceCreateImageAtIndex(source, 0, NULL);
我不使用
- [NSImage中CGImageForProposedRect:背景:提示]因为我的应用程序必须使用10.5 SDK。
当我使用CGContextDrawImage将其绘制到我的NSView中时,它会在文本周围绘制一个透明背景,从而导致窗口后面的任何内容显示。我想我想创建一个剪贴蒙版,但我无法弄清楚如何做到这一点。