Display PDF in CALayer
Display PDF in CALayer
- Subject: Display PDF in CALayer
- From: "Bridger Maxwell" <email@hidden>
- Date: Fri, 11 Jan 2008 23:31:21 -0700
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:
pageNum];
pdfHolder = [[CALayer alloc] init];
pdfHolder.bounds = [pdfHolder convertRect: self.bounds fromLayer: self];
CGPoint innerPos = CGPointMake( CGRectGetMidX(self.bounds),
CGRectGetMidY(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,
kCGPDFMediaBox));
CGContextDrawPDFPage(theContext,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
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden