setNeedsDisplay problem

Discussion in 'iOS Programming' started by kikko088, Jun 30, 2011.

  1. kikko088 macrumors member

    Joined:
    Oct 13, 2010
    Location:
    Italy
    #1
    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?
     
  2. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #2
    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.
     
  3. kikko088, Jul 1, 2011
    Last edited: Jul 1, 2011

    kikko088 thread starter macrumors member

    Joined:
    Oct 13, 2010
    Location:
    Italy
    #3
    I try sending setNeedsDisplay to tiledLayer but norhing, this is what appand:

    [​IMG]

    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.
     
  4. kikko088 thread starter macrumors member

    Joined:
    Oct 13, 2010
    Location:
    Italy

Share This Page