CGContextDrawPDFPage doesn't seem to persist in CGContext
Posted
by erichf
on Stack Overflow
See other posts from Stack Overflow
or by erichf
Published on 2010-04-16T00:28:58Z
Indexed on
2010/04/16
0:33 UTC
Read the original article
Hit count: 685
iphone
I am trying to access the pixels of a CGContext written to with a PDF, but the bitmap buffer doesn't seem to update. Any help would be appreciated:
//Get the reference to our current page
pageRef = CGPDFDocumentGetPage(docRef, iCurrentPage);
//Start with a media crop, but see if we can shrink to smaller crop
CGRect pdfRect1 = CGRectIntegral(CGPDFPageGetBoxRect(pageRef, kCGPDFMediaBox));
CGRect r1 = CGRectIntegral(CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox));
if (!CGRectIsEmpty(r1))
pdfRect1 = r1;
int wide = pdfRect1.size.width + pdfRect1.origin.x;
int high = pdfRect1.size.height + pdfRect1.origin.y;
CGContextRef ctxBuffer = NULL;
CGColorSpaceRef colorSpace;
UInt8* bitmapData;
int bitmapByteCount;
int bitmapBytesPerRow;
bitmapBytesPerRow = (wide * 4);
bitmapByteCount = (bitmapBytesPerRow * high);
colorSpace = CGColorSpaceCreateDeviceRGB();
bitmapData = malloc( bitmapByteCount );
if (bitmapData == NULL)
{
DebugLog (@"Memory not allocated!");
return;
}
ctxBuffer = CGBitmapContextCreate (bitmapData,
wide,
high,
8, // bits per component
bitmapBytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); //
if (ctxBuffer== NULL)
{
free (bitmapData);
DebugLog (@"Context not created!");
return;
}
CGColorSpaceRelease( colorSpace );
//White out the current context
CGContextSetRGBFillColor(ctxBuffer, 1.0, 1.0, 1.0, 1.0);
CGContextFillRect(ctxBuffer, CGContextGetClipBoundingBox(ctxBuffer));
CGContextDrawPDFPage(ctxBuffer, pageRef);
//!!!This displays just fine to the context passed in from - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx. That is, I can see the PDf page rendered, so we know ctxBuffer was created correctly
//However, if I view bitmapData in memory, it only shows as 0xFF (or whatever fill color I use)
CGImageRef img = CGBitmapContextCreateImage(ctxBuffer);
CGContextDrawImage(ctx, tiledLayer.frame, img);
void *data = CGBitmapContextGetData (ctx);
for (int i = 0; i < wide; i++)
{
for (int j = 0; j < high; j++)
{
//All of the bytes show as 0xFF (or whatever fill color I test with)?!
int byteIndex = (j * 4) + i * 4;
UInt8 red = bitmapData[byteIndex];
UInt8 green = bitmapData[byteIndex + 1];
UInt8 blue = bitmapData[byteIndex + 2];
UInt8 alpha = m_PixelBuf[byteIndex + 3];
}
}
I have also tried using CGDataProviderCopyData(CGImageGetDataProvider(img)) & CFDataGetBytePtr, but the results are the same?
© Stack Overflow or respective owner