2010-04-16 59 views
0

我想要訪問用PDF寫入到CGContext的像素,但位圖緩衝區似乎沒有更新。任何幫助,將不勝感激:CGContextDrawPDFPage似乎不會持續在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)) & CFDataGetBytePtr也試過,但結果是一樣的嗎?

回答

0

Bonehead獎給我。循環指數不正確(j * 4)=>(j * bitmapBytesPerRow)