我正在尝试访问用PDF写入的CGContext
像素,但位图缓冲区似乎没有更新。任何帮助将不胜感激:
//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);
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];
}
}
我也尝试过使用CGDataProviderCopyData(CGImageGetDataProvider(img))
&amp; CFDataGetBytePtr
,但结果是一样的吗?
答案 0 :(得分:0)
Bonehead奖励给我。循环索引不正确(j * 4)=&gt; (j * bitmapBytesPerRow)