tags:

views:

218

answers:

1

Hi guys, i'm a noob in IPhone sdk.

I try to develop a custom pdf read using CatiledLayer but i've many problem. I can't load all the pages of pdf document so i try to draw dynamically the pages but this code of draw function don't work:

- (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]);

 }
}

this function print only the last page.

Any idea?'

the magic number is't correct the rigth code is:

- (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;







}

Now all work !!

A: 

It looks like you're doing CGContextFillRect for the entire context each time around the for loop, thus blanking out the existing material (i.e. all but the last page).

I am trying to wrap my head around PDF drawing in CATiledLayer myself. You seem to have a "magic number" where you translate your coordinate system; (5-2*i)/12. I assume this is for lining up the pages next to one another. Are you sure those numbers are correct?

Marcus