Core Graphics: Creating a bitmap from an array

Status
Not open for further replies.

Mark Cianciosa

Wise, Aged Ars Veteran
161
I'm trying to draw an image from an array of data. Simplified the code looks like<BR><BR><pre class="ip-ubbcode-code-pre">
void drawDataInRect(CGContextRef context, CGRect frame) {
size_t width = 100;
size_t height = 100;

CGFloat bitMapData[width*height*4];
for (size_t i = 0, e = height*width*4; i < e;) {
// This would be replaced with the actual data converted to RGB components.
bitMapData[i++] = 1.0f;
bitMapData[i++] = 0.0f;
bitMapData[i++] = 0.0f;
bitMapData[i++] = 1.0f;
}

CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
CGContextRef bitMapContext = CGBitmapContextCreate(bitMapData, width, height, 16, 4*width, colorSpace, kCGImageAlphaLast | kCGBitmapFloatComponents);
CGColorSpaceRelease(colorSpace);

CGImageRef bitMapImage = CGBitmapContextCreateImage(bitMapContext);
CGContextDrawImage(context, frame, bitMapImage);

CGImageRelease(bitMapImage);
CGContextRelease(bitMapContext);
}
</pre> <BR><BR>How this just produces a blank image. What is the easiest way to draw an array of pixel values?
 

andrewme

Senator
7,120
Subscriptor
Looking at the API reference, I would say CGBitmapContextCreate is not the function you're looking for. It doesn't draw an image from a given array of pixels - it creates a drawing context and whatever operations you perform on that context get drawn <I>into</I> the given memory.<BR><BR>It looks like what you want would be CGImageCreate to make the image, CGDataProviderCreateWithCFData to construct it from a pixel array, and CFDataCreateWithBytesNoCopy to wrap an your buffer as a CFData object.
 
Status
Not open for further replies.