Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

kikko088

macrumors member
Original poster
Oct 13, 2010
77
0
Italy
Hi at all, I try to do a pdf viewer with multipage and zoom, the zoom work while the multipage has some problem, if I change the page the page not change but if I zoom the view change and I view the correct page, if I put zoomscale=1 I see the wrong page.

this is my code:

Code:
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    currentPage=1;
    
    NSString *pathToPdfDoc = [[NSBundle mainBundle] pathForResource:@"myPDF" ofType:@"pdf"];
    NSURL *pdfUrl = [NSURL fileURLWithPath:pathToPdfDoc];
    myDocumentRef = CGPDFDocumentCreateWithURL((CFURLRef)pdfUrl);
    
    CGRect pageRect = CGRectMake(0, 0, 320, 480);
    
    tiledLayer = [CATiledLayer layer];
    tiledLayer.delegate = self;
    tiledLayer.tileSize = CGSizeMake(1024.0, 1024.0);
    tiledLayer.levelsOfDetail = 1000;
    tiledLayer.levelsOfDetailBias = 1000;
    tiledLayer.frame = pageRect;
    
    pdfView = [[UIView alloc] initWithFrame:pageRect];
    [pdfView.layer addSublayer:tiledLayer];
    
    CGRect viewFrame = self.view.frame;
    viewFrame.origin = CGPointZero;
    scrollView = [[UIScrollView alloc] initWithFrame:viewFrame];
    scrollView.delegate = self;
    scrollView.contentSize = pageRect.size;
    scrollView.maximumZoomScale = 1000;
    [scrollView addSubview:pdfView];
    
    [self.view addSubview:scrollView];
    
    [self gesture];
    
    
}


- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return pdfView;
}

- (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
{
    CGPDFPageRef page = CGPDFDocumentGetPage(myDocumentRef, currentPage);
    CGContextSaveGState(ctx);
    
    CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
    CGContextScaleCTM(ctx, 1.0, -1.0);
    CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, layer.bounds, 0, true));
    CGContextDrawPDFPage(ctx, page);
    CGContextRestoreGState(ctx);
}

-(void)increasePageNumber {
    size_t pageCount = CGPDFDocumentGetNumberOfPages(myDocumentRef);
    if (currentPage == pageCount) {
        // do nothing
    }
    else {
        currentPage++;
        [pdfView setNeedsDisplay];
    }
}

where is the mistake for you?
 
Your so-called pdfView is a misnomer. The PDF is not actually being drawn in that view. It's being drawn in the tiledLayer Core Animation sublayer of pdfView.

Try sending setNeedsDisplay to tiledLayer, not pdfView.
 
I try sending setNeedsDisplay to tiledLayer but norhing, this is what appand:



when I zoom i see correct page and wrong page

EDIT: if I put all the code on viewDidAppear, I change the page, I go to another controller then return on pdfcontroller I see the correct page.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.