I am trying to create an RGBA8 image from text for 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; }
Unfortunately, there is 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.
What am I missing to create the right RGBA8 format that OpenGL needs here?
Update: I changed the last CGBitmapContextCreate parameter from kCGImageAlphaNone (which was when copying the code) to kCGImageAlphaLast , which is one of several options that I tried with an error.
Update 2: UIGraphicsGetImageFromCurrentImageContext() returns nil if the context was not created using UIGraphicsBeginImageContext() , so you need to extract the image in a different way: [UIImage imageWithCGImage:CGBitmapContextCreateImage(contextRef)] .
Ian terrell
source share