Cocoa在NSImage上绘制文本 [英] Cocoa draw text on NSImage
问题描述
我有一个NSSegmentedControl(todayButton)包含一个图像。我试图使用以下绘制一些文本到图像上:
I have an NSSegmentedControl(todayButton) that contains an image. I'm trying to draw some text onto the image using the following:
NSImage *img = [todayButton imageForSegment:0]
[img lockFocus]
[@"15" drawAtPoint:NSZeroPoint withAttributes:nil]
[img unlockFocus]
[img setTemplate:YES]
图像设置为模板,不会抛出任何错误, p>
The image gets set as a template and no errors are thrown but no text appears on the image.
推荐答案
我可能完全错了,但我的直觉告诉我你可能会遇到NSImage缓存问题。除非您更改映像的设置(通常不是要重新使用的映像的最佳方法),否则系统会缓存映像,或者只是创建一个映像,而不是从捆绑包加载映像。
I may be entirely wrong, but my gut tells me you may be running into NSImage caching issues. The system caches images unless you change the settings for an image (usually not the best approach for an image you want to reuse) or simply create a new one versus loading one from the bundle.
您可能需要从头开始制作新图片,将任何背景(您从按钮段获得的内容)复合到其中,然后绘制文本。否则系统会缓存它在别处获得的东西,这有时会导致未定义的(或至少是意外的)行为。
You may need to make a new image from scratch, composite whatever background (what you were getting from your button segment) into it, then draw the text. Otherwise the system is caching something it got elsewhere, which can sometimes lead to undefined (or at least unexpected) behavior.
这篇关于Cocoa在NSImage上绘制文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!