Display PDF in CALayer

  • Hey,
      I have been experimenting around with Core Animation.  I am making a
    program where you can display various files as Core Animation layers, like
    pictures, movies, or PDFs.  Almost like Coverflow does it (if Coverflow uses
    CA).  I am having trouble displaying PDFs though.  The problem I have is
    with resizing, I would like to be able to set the scale on a layer
    containing a PDF, or a parent of that layer, and "zoom in" on the text.  I
    have finally been able to draw the PDF within the bounds of a layer, but
    when I zoom in it becomes no clearer, and if I am already zoomed in when it
    is draw it starts out with bad quality.  I think what I want is to draw the
    whole PDF in a CA layer, then resize it down to a reasonable size.  Then
    when the CALayer size changes, it will show sharper detail.
      Right now I have a "parent" layer which will draw a border, and page
    navigation, and it has a layer within it which will contain the PDF
    rendering. Here is my code thus far:

    This first code segment is from the parent layer.  It creates a the PDF
    layer, and creates and object to which it can delegate the drawing code for
    that PDF layer.
        PDFPainter = [[CBDocumentPDFDelegate alloc] initWithPDF: pdf page:
        pdfHolder = [[CALayer alloc] init];
        pdfHolder.bounds = [pdfHolder convertRect: self.bounds fromLayer: self];
        CGPoint innerPos = CGPointMake( CGRectGetMidX(self.bounds),
        pdfHolder.position = innerPos;

        [pdfHolder setDelegate: PDFPainter];
        pdfHolder.needsDisplayOnBoundsChange = YES;
        [pdfHolder setNeedsDisplay];
        [self addSublayer: (id) pdfHolder];

    Here is the drawing code from the CBDocumentPDFDelegate class:
    - (void)drawLayer:(CALayer *)layer inContext:(CGContextRef)theContext {

        CGColorRef background = CGColorGetConstantColor(kCGColorWhite);
        layer.backgroundColor = background;

        CGPDFPageRef page = CGPDFDocumentGetPage(layerPDF, currentPage);

        CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page,
    kCGPDFMediaBox,layer.bounds, 0, true);
        CGContextSaveGState (theContext);
        CGContextConcatCTM (theContext, pdfTransform);
        CGContextClipToRect (theContext, CGPDFPageGetBoxRect(page,
        CGContextRestoreGState (theContext);

    Remember, I am a new Cocoa programmer (I don't think I can even call myself
    that yet), so be gentle.  :)

      Thank You,
        Bridger Maxwell
previous month january 2008 next month
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today