如何在Core Graphics中生成RGBA图像? [英] How do you generate an RGBA image in Core Graphics?

查看:147
本文介绍了如何在Core Graphics中生成RGBA图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从文本生成RGBA8图像以用作OpenGL ES 2.0纹理。

I'm trying to generate an RGBA8 image from text to use as an OpenGL ES 2.0 texture.

+(UIImage *)imageFromText:(NSString *)text
{
  UIFont *font = [UIFont systemFontOfSize:20.0];  
  CGSize size  = [text sizeWithFont:font];

  CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  CGContextRef contextRef =  CGBitmapContextCreate (NULL,
                                                    size.width, size.height,
                                                    8, 4*size.width,
                                                    colorSpace,
                                                    kCGImageAlphaLast
                                                    );
  CGColorSpaceRelease(colorSpace);
  UIGraphicsPushContext(contextRef);

  [text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font];
  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsPopContext();

  return image;
}

不幸的是,没有 CGColorSpaceCreateDeviceRGBA CGColorSpaceCreateDeviceRGB 导致以下错误:

Unfortunately, there's no CGColorSpaceCreateDeviceRGBA, and CGColorSpaceCreateDeviceRGB results in the following error:

CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaLast; 448 bytes/row.

创建OpenGL想要的正确RGBA8格式我缺少什么?

What am I missing to create the proper RGBA8 format that OpenGL wants here?

更新:我从 kCGImageAlphaNone CGBitmapContextCreate 的最后一个参数$ c>(当我复制粘贴代码时)是 kCGImageAlphaLast ,这是我错误尝试过的几种变体之一。

Update: I changed the last parameter of CGBitmapContextCreate from kCGImageAlphaNone (which it was when I copy pasted the code) to kCGImageAlphaLast, which is one of several variations I've tried in error.

Update 2 UIGraphicsGetImageFromCurrentImageContext()如果未使用 UIGraphicsBeginImageContext创建上下文,则返回nil (),因此有必要以不同方式提取图像: [UIImage imageWithCGImage:CGBitmapContextCreateImage(contextRef)]

Update 2: UIGraphicsGetImageFromCurrentImageContext() returns nil if the context was not created with UIGraphicsBeginImageContext(), so it is necessary to extract the image differently: [UIImage imageWithCGImage:CGBitmapContextCreateImage(contextRef)].

推荐答案

您在创建过程中指定的颜色空间不会导致类似的错误。

The color space you specify during creation wouldn't cause an error like that.

你得到那个错误的原因是你为每个组件指定了8位,可能是 4 * size.width bytesPerRow 传入的值,但 kCGImageAlphaNone的 bitmapInfo 参数 kCGImageAlphaNone 仅表示 RGB ,而不是 RGBA 。如果你想要 RGBA ,你应该很可能指定 kCGImageAlphaLast kCGImageAlphaPremultipliedLast

The reason you're getting that error is that you've specified 8 bits per component, presumably 4 color components in the 4*size.width value you passed in for bytesPerRow, yet a bitmapInfo parameter of kCGImageAlphaNone. kCGImageAlphaNone means only RGB, not RGBA. If you want RGBA, you should most likely specify kCGImageAlphaLast kCGImageAlphaPremultipliedLast.

抱歉。我应该说 kCGImageAlphaPremultipliedLast ,而不是 kCGImageAlphaLast

sorry. I should have said kCGImageAlphaPremultipliedLast, not kCGImageAlphaLast.

所以,像这样:

CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
CGContextRef contextRef =  CGBitmapContextCreate(NULL,
                                                 size.width,
                                                 size.height,
                                                 8,
                                                 4 * size.width,
                                                 colorSpace,
                                                 kCGImageAlphaPremultipliedLast);

这篇关于如何在Core Graphics中生成RGBA图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆