0
我发现应该使用UIFont绘制文本的代码。我想从中获取UIImage以确保它能够正常工作。iOS - 从CGBitmapContext获取UIImage
后来我得到一个OpenGL纹理我的需求,但由于其没有显示任何我喜欢验证代码的工作:
NSUInteger width, height, i;
CGContextRef context;
void *data;
CGColorSpaceRef colorSpace;
UIFont *font;
font = [UIFont fontWithName:name size:size];
width = (NSUInteger)dimensions.width;
if ((width != 1) && (width & (width - 1))) {
i = 1;
while (i < width)
i *= 2;
width = i;
}
height = (NSUInteger)dimensions.height;
if ((height != 1) && (height & (height - 1))) {
i = 1;
while (i < height)
i *= 2;
height = i;
}
colorSpace = CGColorSpaceCreateDeviceGray();
data = calloc(height, width);
context = CGBitmapContextCreate(data, width, height, 8, width, colorSpace, kCGImageAlphaNone);
CGColorSpaceRelease(colorSpace);
CGContextSetGrayFillColor(context, 1.0, 1.0);
CGContextTranslateCTM(context, 0.0, height);
CGContextScaleCTM(context, 1.0f, -1.0f); //NOTE: NSString draws in UIKit referential i.e. renders upside-down compared to CGBitmapContext referential
UIGraphicsPushContext(context);
NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[style setLineBreakMode:NSLineBreakByWordWrapping];
NSDictionary *attrsDictionary = @{
NSFontAttributeName: font,
NSBaselineOffsetAttributeName: @1.0F,
NSParagraphStyleAttributeName: style
};
[string drawInRect:CGRectMake(0, 0, dimensions.width, dimensions.height)
withAttributes:attrsDictionary];
UIGraphicsPopContext();
// Here I use "void *data" to obtain an OpenGL texture
// ...
// ...
CGContextRelease(context);
free(data);
非常感谢提前,任何帮助,将不胜感激!