【发布时间】:2009-10-08 02:03:30
【问题描述】:
我正在尝试获取 NSString 并将其光栅化为位图(字节数组 (RGBA))
我最接近关于如何做这样的事情的任何信息是在一个论坛帖子中,其中提出了一些类似的内容
NSString *myString = [[NSString alloc] initWithString:@"This is a test"];
NSSize size = {256, 256};
NSImage *myImage = [[NSImage alloc] initWithSize:size];
[myImage lockFocus];
// if you have a second image you're going to overlay on top of the first, do the same except use NSCompositeSourceOver as the operation
[myString drawAtPoint: NSMakePoint(0, 0)];
[myImage unlockFocus];
NSData *tiffData = [myImage TIFFRepresentation];
不幸的是,应用在 [myImage lockFocus] 处冻结/崩溃。
我也在 CoreGraphics 中研究过这样做,但我发现的信息更少。我得到了点点滴滴,即位图上下文和字体对象,但没有任何暗示要“光栅化这个字符串”
如果有人对如何做到这一点有一些想法,将不胜感激
谢谢。
[编辑] 我刚刚注意到this,这就是我很明显这对我来说是新的。我是否可以使用 CGBitmapContextCreate 创建一个 CGContextRef,然后使用 UIGraphicsPushContext “推送”它,以便任何后续的 drawAtPoint 呈现到该上下文引用?抱歉,我现在想大声说出来。我应该试试看。 [编辑2] UIGraphicsPushContext 是一种 UIKit 方法,不幸的是,据我所知,这意味着 iPhone。
【问题讨论】: