How do I create a CGImage with RGB data?

ReachConnection picture ReachConnection · Oct 14, 2009 · Viewed 13.5k times · Source

Can anyone show me a clear example of how to use

CGImageRef CGImageCreate 
(
   size_t width,
   size_t height,
   size_t bitsPerComponent,
   size_t bitsPerPixel,
   size_t bytesPerRow,
   CGColorSpaceRef colorspace,
   CGBitmapInfo bitmapInfo,
   CGDataProviderRef provider,
   const CGFloat decode[],
   bool shouldInterpolate,
   CGColorRenderingIntent intent
);

I have a CGFloat dataBuffer [width * height * 3];

It contains all of the RGB data of the image. Essentially, it goes from pixel R (0,0), G (0,0), B(0,0), R(0,1)... to R(width, height), G(width,height), B(width,height)

I know the size of the image, width x height. How do I use the information that I have and create and CGImage??

Please help, thanks

I posted another question that encompasses the above question. It was answered correctly by nschmidt, here is the link

Converting RGB data into a bitmap in Objective-C++ Cocoa

Answer

Peter Hosey picture Peter Hosey · Oct 17, 2009

The biggest mystery there is the data provider. The answer for that is that you need to create a CGDataProvider object that either contains or can provide the pixel data you have.

Some lesser mysteries are what to pass for colorSpace and bitmapInfo. Those are a CGColorSpace object and a CGBitmapInfo bit-mask.

The explanation of the decode array could be clearer, but what is clear is that you probably don't need one. Pass NULL.

Aside from those things, everything is either self-evident or well-explained in the documentation for this function.