title: How do you generate an RGBA image in Core Graphics?
body: <p>I'm trying to generate an RGBA8 image from text to use as an OpenGL ES 2.0 texture.</p>
  
  <pre><code>+(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;
  }
  </code></pre>
  
  <p>Unfortunately, there's no <code>CGColorSpaceCreateDeviceRGBA</code>, and <code>CGColorSpaceCreateDeviceRGB</code> results in the following error:</p>
  
  <pre><code>CGBitmapContextCreate: unsupported parameter combination: 8 integer bits/component; 32 bits/pixel; 3-component color space; kCGImageAlphaLast; 448 bytes/row.
  </code></pre>
  
  <p>What am I missing to create the proper RGBA8 format that OpenGL wants here?</p>
  
  <p><em>Update</em>: I changed the last parameter of <code>CGBitmapContextCreate</code> from <code>kCGImageAlphaNone</code> (which it was when I copy pasted the code) to <code>kCGImageAlphaLast</code>, which is one of several variations I've tried in error.</p>
  
  <p><em>Update 2</em>: <code>UIGraphicsGetImageFromCurrentImageContext()</code> returns nil if the context was not created with <code>UIGraphicsBeginImageContext()</code>, so it is necessary to extract the image differently: <code>[UIImage imageWithCGImage:CGBitmapContextCreateImage(contextRef)]</code>.</p>