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

查看:22
本文介绍了如何在 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;
}

很遗憾,没有CGColorSpaceCreateDeviceRGBACGColorSpaceCreateDeviceRGB导致如下错误:

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?

更新:我将 CGBitmapContextCreate 的最后一个参数从 kCGImageAlphaNone (当我复制粘贴代码时)更改为 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.

更新 2:如果上下文不是使用 UIGraphicsBeginImageContext() 创建的,UIGraphicsGetImageFromCurrentImageContext() 返回 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 位,大概是您为 bytesPerRow<传递的 4*size.width 值中的 4 个颜色组件/code>,但 kCGImageAlphaNonebitmapInfo 参数.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天全站免登陆