2010-07-15 66 views
1

我是iPhone SDK的noob。iPhone CatiledLayer pdf

我嘗試使用CatiledLayer開發自定義PDF閱讀,但我遇到了很多問題。 我無法加載PDF文檔的所有頁面,所以我嘗試動態繪製網頁,但繪製函數的代碼不起作用:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 
{ 
CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0); 
CGContextSaveGState(ctx); 
for(int i=0; i<4;i++) 
{ 

CGContextRestoreGState(ctx); 
CGContextSaveGState(ctx); 
    CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx)); 
    CGContextTranslateCTM(ctx, -layer.bounds.size.width*((5-2*i)/12), layer.bounds.size.height); 
    CGContextScaleCTM(ctx, 1.0, -1.0); 
    CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform([[pages objectAtIndex:i] pagin], kCGPDFCropBox, layer.bounds, 0, true)); 
    CGContextDrawPDFPage(ctx, [[pages objectAtIndex:i] pagin]); 

} 
} 

此功能只打印了最後一頁。

有什麼想法?

幻數豈不等於正確 的分辯代碼:

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx 
{ 



    //scrollView.scrollEnabled=NO; 
    //scrollView.alpha=0.2; 
    //[scrollView setContentOffset:scrollView.contentOffset animated:YES]; 
    CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0); 
    if(attua==1) 
    { 
     CGContextSaveGState(ctx); 
     CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx)); 
     CGContextTranslateCTM(ctx,layer.bounds.size.width*1/(2*[pages count]) -layer.bounds.size.width*[pages count]/(2*[pages count]), layer.bounds.size.height); 
     CGContextScaleCTM(ctx, 1.0, -1.0); 
     CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform([[pages objectAtIndex:0] pagin], kCGPDFCropBox, layer.bounds, 0, true)); 
     CGContextDrawPDFPage(ctx, [[pages objectAtIndex:0] pagin]); 

     CGContextRestoreGState(ctx); 
    } 


    for(int i=1; i<[pages count];i++) 

    {   //myContentView.frame=CGRectMake(0, 0,800*i, 1024); 


     if(i>attua-2) 
     { 
      if(i<attua+2) 
      { 


       CGContextSaveGState(ctx); 
       CGContextTranslateCTM(ctx,layer.bounds.size.width*(i*2+3/2)/(2*[pages count]) -layer.bounds.size.width*[pages count]/(2*[pages count]), layer.bounds.size.height); 
       CGContextScaleCTM(ctx, 1.0, -1.0); 
       CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform([[pages objectAtIndex:i] pagin], 0, layer.bounds, 0, true)); 
       CGContextDrawPDFPage(ctx,[[pages objectAtIndex:i] pagin]); 

       CGContextRestoreGState(ctx); 
      } 
     } 


    } 
    scrollView.scrollEnabled=YES; 







} 

現在所有的工作!

回答

1

看起來您每次圍繞for循環時都要爲整個上下文執行CGContextFillRect,因此會清空現有材質(即除最後一頁以外的所有材質)。

我想繞過PDF畫在我自己的CATiledLayer周圍。你似乎有一個「幻數」,你翻譯你的座標系; (5-2 * I)/ 12。我認爲這是爲了排列彼此相鄰的頁面。你確定這些數字是正確的嗎?