我在drawRect中有一个可以渲染成UIView的CGContext的应用程序。我还使用后台渲染器导出这些渲染。它使用相同的渲染逻辑(比实时更快)渲染成CGBitmapContext (我随后将其转换为mp4文件)。
我注意到输出的视频有一些奇怪的小故障。例如图像被旋转,渲染图像的奇怪复制,随机噪声,以及时间也是奇怪的。
我正在寻找调试这个的方法。对于时间问题,我想我应该呈现一个字符串,它告诉我我当前正在查看哪个帧,结果却发现将文本呈现到CGContext中并没有很好的文档。事实上,围绕核心图形的文档对我的一些经验来说是相当不可原谅的。
因此,具体地说,我想知道如何在上下文中呈现文本。如果它的核心文本,它必须互操作一些如何与核心图形上下文?一般来说,我会感谢任何关于位图渲染和调试结果的提示和建议。
发布于 2019-02-22 14:38:15
根据另一个问题:How to convert Text to Image in Cocoa Objective-C
我们可以使用CTLineDraw在CGBitmapContext示例代码中绘制文本:
NSString* string = @"terry.wang";
CGFloat fontSize = 10.0f;
// Create an attributed string with string and font information
CTFontRef font = CTFontCreateWithName(CFSTR("Helvetica Light"), fontSize, nil);
NSDictionary* attributes = [NSDictionary dictionaryWithObjectsAndKeys:
(id)font, kCTFontAttributeName,
nil];
NSAttributedString* as = [[NSAttributedString alloc] initWithString:string attributes:attributes];
CFRelease(font);
// Figure out how big an image we need
CTLineRef line = CTLineCreateWithAttributedString((CFAttributedStringRef)as);
CGFloat ascent, descent, leading;
double fWidth = CTLineGetTypographicBounds(line, &ascent, &descent, &leading);
// On iOS 4.0 and Mac OS X v10.6 you can pass null for data
size_t width = (size_t)ceilf(fWidth);
size_t height = (size_t)ceilf(ascent + descent);
void* data = malloc(width*height*4);
// Create the context and fill it with white background
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast;
CGContextRef ctx = CGBitmapContextCreate(data, width, height, 8, width*4, space, bitmapInfo);
CGColorSpaceRelease(space);
CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0); // white background
CGContextFillRect(ctx, CGRectMake(0.0, 0.0, width, height));
// Draw the text
CGFloat x = 0.0;
CGFloat y = descent;
CGContextSetTextPosition(ctx, x, y);
CTLineDraw(line, ctx);
CFRelease(line);https://stackoverflow.com/questions/49931870
复制相似问题