开发者

How to create a clipping mask from an NSAttributedString?

I have an NSAttributedString which I would like to draw into a CGImage so that I can later draw the CGImage into an NSView. Here's what I have so far:

// Draw attributed string into NSImage
NSImage* cacheImage = 开发者_StackOverflow中文版[[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);

I'm not using -[NSImage CGImageForProposedRect:context:hints] because my app must use the 10.5 sdk.

When I draw this into my NSView using CGContextDrawImage, it draws a transparent background around the text, causing whatever is behind the window to show through. I think I want to create a clipping mask, but I can't figure out how to do that.


It sounds like your blend mode it set up as Copy instead of SourceOver. Take a look at the CoreGraphics blend mode documentation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜