1

I have an array that contains the RGB colour values for each pixel in a 320 x 180 display. I would like to be able to set individual pixel values in the a bitmap context of the same size offscreen then display the bitmap context in a view.

It appears that I have to create 1x1 rects and either put a stroke on them or a line of length 1 at the point in question. Is that correct? I'm looking for a very efficient way of getting the array data onto the graphics context as you can imagine this is going to be an image buffer that cycles at 25 frames per second and drawing in this way seems inefficient.

I guess the other question is should I use OPENGL ES instead?

Thoughts/best practice would be much appreciated.

Regards

Dave

OK, have come a short way, but can't make the final hurdle and I am not sure why this isn't working:

- (void) displayContentsOfArray1UsingBitmap: (CGContextRef)context
{
    long bitmapData[WIDTH * HEIGHT];

    // Build bitmap
    int i, j, h;
    for (i = 0; i < WIDTH; i++)
    {
        for (j = 0; j < HEIGHT; j++)
        {
        h = frameBuffer01[i][j];
        bitmapData[i * j] = h;
        }
}
// Blit the bitmap to the context
    CGDataProviderRef providerRef = CGDataProviderCreateWithData(NULL, bitmapData,4 * WIDTH * HEIGHT, NULL);

    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();

    CGImageRef imageRef = CGImageCreate(WIDTH, HEIGHT, 8, 32, WIDTH * 4, colorSpaceRef, kCGImageAlphaFirst, providerRef, NULL, YES, kCGRenderingIntentDefault);

    CGContextDrawImage(context, CGRectMake(0.0, HEIGHT, WIDTH, HEIGHT), imageRef);

    CGImageRelease(imageRef);
    CGColorSpaceRelease(colorSpaceRef);
    CGDataProviderRelease(providerRef);
}

2 Answers 2

2

Read the documentation for CGImageCreate(). Basically, you have to create a CGDataProvider from your pixel array (using CGDataProviderCreateDirect()), then create a CGImage with this data provider as a source. You can then draw the image into any context. It's a bit tedious to get this right because these functions expect a lot of arguments, but the documentation is quite good.

2
  • Cheers Ole, will give it a go. Dave. Mar 7, 2010 at 17:51
  • Hi Ole, sorry to bother you, but any ideas what is wrong with my code above? Cheers Dave. Mar 25, 2010 at 8:06
1

Dave,

The blitting code works fine, but your code to copy from the frame buffer is incorrect.

// Build bitmap
int i, j, h;
for (i = 0; i < WIDTH; i++)
{
    for (j = 0; j < HEIGHT; j++)
    {
    h = frameBuffer01[i][j];
    bitmapData[/*step across a line*/i + /*step down a line*/j*WIDTH] = h;
    }
}

Note my changes to the assignment to elements of bitmapData.

Not knowing the layout of frame, this may still be incorrect, but from your code, this looks closer to the intent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.