Drawing into a CGContextRef created using CGBitmapContextCreate
So, if you happen to be doing what the subject line says – drawing anything into a CGContextRef that was created using CGBitmapContextCreate – and you’re seeing nothing but black, the trick is the following: you have to memset the the pixel data to all 0xFF’s before you draw.
For example, to draw a PDF document reference by ‘document’:
// allocate pixels
unsigned char* pixels = new unsigned char[thumb_height*thumb_width*4];
// create context
CGContextRef context = CGBitmapContextCreate(pixels, thumb_width, thumb_height, 8, 4*thumb_width, CGColorSpaceCreateWithName( kCGColorSpaceGenericRGB ), kCGImageAlphaNoneSkipLast );
// clear
memset( pixels, 255, thumb_width*thumb_height*4 );
// render page 1 into the pixels array
CGPDFPageRef page = CGPDFDocumentGetPage( document, 1 );
CGContextDrawPDFPage( context, page );
(For some reason I just wasn’t able to create any kind of RGBA context with CGBitmapContextCreate() – I always had to use the skip-alpha enum. Weird.)
![damian [at] frey [dot] co [dot] nz](http://frey.co.nz/old/wp-content/themes/freynew/email.jpg)

February 24th, 2009 at 12:37 pm
Many Thanks.
My pdf rendering remains desesperaly black until you light my brain.
CGPDFDocumentGetPage probably compose over the current bitmap layer.