CGPDFViewController and multipage PDF (zooming)

Discussion in 'iPhone/iPad Programming' started by Soulstorm, May 4, 2011.

  1. macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #1
    I am trying to create a basic PDF viewer for iOS, and I am pretty much stuck. I only want it to be able to view PDFs and jump to a specific page.

    Here is what I have done so far:

    Code:
    #import "SFPDFViewerController.h"
    #import <QuartzCore/QuartzCore.h>
    
    @implementation SFPDFViewerController
    @synthesize currentPageIndex;
    
    - (void)viewDidLoad
    {
        currentPageIndex = 1;
    
        myDocumentRef = CGPDFDocumentCreateWithURL((CFURLRef)[NSURL URLWithString:@"file:///Users/soulstorm/Desktop/Basic%20UNIX%20Tutorial.pdf"]);
        myPageRef = CGPDFDocumentGetPage(myDocumentRef, currentPageIndex);
        //CGRect pageRect = CGRectIntegral(CGPDFPageGetBoxRect(myPageRef, kCGPDFCropBox));
        CGRect pageRect = self.view.bounds;
    
        CATiledLayer *tiledLayer = [CATiledLayer layer];
        tiledLayer.delegate = self;
        tiledLayer.tileSize = CGSizeMake(200.0, 200.0);
        tiledLayer.levelsOfDetail = 1000;
        tiledLayer.levelsOfDetailBias = 1000;
        tiledLayer.frame = pageRect;
    
        myContentView = [[UIView alloc] initWithFrame:pageRect];
        [myContentView.layer addSublayer:tiledLayer];
    
        CGRect viewFrame = self.view.frame;
        viewFrame.origin = CGPointZero;
        viewFrame.size.height-= 44;
    
        scrollView = [[UIScrollView alloc] initWithFrame:viewFrame];
        scrollView.delegate = self;
        scrollView.contentSize = pageRect.size;
        scrollView.maximumZoomScale = 3.0;
        [scrollView addSubview:myContentView];
    
        [self.view addSubview:scrollView];
    
        [self.view setBackgroundColor:[UIColor whiteColor]];
        [scrollView setBackgroundColor:[UIColor whiteColor]];
    }
    
    - (void)viewDidUnload
    {
        [super viewDidUnload];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
    {
        return myContentView;
    }
    
    - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx
    {
        NSLog(@"draw layer...");
        CGContextSetRGBFillColor(ctx, 1.0, 1.0, 1.0, 1.0);
        CGContextFillRect(ctx, CGContextGetClipBoundingBox(ctx));
        CGContextTranslateCTM(ctx, 0.0, layer.bounds.size.height);
        CGContextScaleCTM(ctx, 1.0, -1.0);
        CGContextConcatCTM(ctx, CGPDFPageGetDrawingTransform(myPageRef, kCGPDFCropBox, layer.bounds, 0, true));
        CGContextDrawPDFPage(ctx, myPageRef);
    }
    
    #pragma mark - Functions
    - (IBAction)goToNextPage:(id)sender
    {
        [self displayNextPage];
    }
    - (IBAction)goToPreviewsPage:(id)sender
    {
        [self displayPreviewsPage];
    }
    
    - (void)displayNextPage
    {
        currentPageIndex++;
        [self reloadCurrentPage];
    }
    - (void)displayPreviewsPage
    {
        currentPageIndex--;
        [self reloadCurrentPage];
    }
    - (void)reloadCurrentPage
    {
        NSLog(@"reloading current page... %i", currentPageIndex);
        myPageRef = CGPDFDocumentGetPage(myDocumentRef, currentPageIndex);
        //[self.view setNeedsDisplay];
        //[self.view.layer setNeedsDisplay];
        //[myContentView.layer setNeedsDisplay];
    }
    #pragma mark -
    - (void)dealloc
    {
        [myContentView release];
        myContentView = nil;
        CGPDFDocumentRelease(myDocumentRef);
        myDocumentRef = NULL;
        myPageRef = NULL;
        [super dealloc];
    }
    @end
    The above code displays the first page of the PDF corectly, but when I try to go to the next page, it will remain on the first one, and sometimes when zooming I can see parts of the second page.

    Can anyone point me in the right direction?
     
  2. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #2
    Look for the library called Leaves, it's on google groups called, Leaves-Developers :>
     
  3. macrumors newbie

    Joined:
    Sep 14, 2010
    #3
    PDF viewing

    Seems to me that what you're attempting is already provided for you in QuickLook (introduced in iOS 4.1, I believe)
    :apple:
     
  4. thread starter macrumors 68000

    Soulstorm

    Joined:
    Feb 1, 2005
    #4
    Indeed, but I think that Quicklook does not provide jumping to a specific page inside a document...
     
  5. macrumors 6502

    Joined:
    Jan 13, 2011
    Location:
    Los Angeles, CA
    #5
    I could be wrong, but isn't this how Apple implements PDF viewing in iBooks? And in iBooks there is a way to jump to a specific page. More than one way actually.
     
  6. macrumors 65816

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #6
    I'm interested in a basic tutorial from this QuickLook thing, could allways be usefull ;)
    Thanksies! :)
     

Share This Page