views:

47

answers:

3

I have a PDFDocument (made up of PDFPages ofcourse) and I need a CGImageRef to stick into my IKImageView.

Currently, I get the datarepresentation from the PDFPage, put it into an NSImage, then get the TIFFRepresentation of the NSImage, put it into a CGImageSource, and then get the CGImage out of the source.

That seemsneedlessly complicated, going through two NSData steps...

However, when I try to put the PDF data directly into the CGImageSource, even though that APPEARS to work, when I get the CGImage out, it is always NULL. is there something specific I should be doing to get this to work?

+1  A: 

In Snow Leopard there is a method -[NSImage CGImageForProposedRect:context:hints:]. Or you could draw the page into a CGBitmapContext and use CGBitmapContextCreateImage.

JWWalker
Sorry, I forgot to mention that this needs to work in 10.5... if I could avoid the NSImage stage entirely, that would be great. the cgbitmapcontext strategy might work better... I'll see how nasty that becomes.
Brian Postow
A: 

Look at the CreatePDFPageImage function in my CGPDFAdditions code. I think that's exactly what you need.

0xced
Interesting. Is there an easy way to get a CGPDFPage from a PDFPage? or a CGPDFDocument from a PDFDocument? If I have to turn the PDFPage into data, and then make a provider to get a CGPDFDocument, and then get the page out of that, I'm not sure that it's that much of a win over my current NSImage solution...
Brian Postow
There is an undocumented `-(CGPDFPageRef)pageRef;` method in the `PDFPageInternal` category of the `PDFPage` class, you may try this. Otherwise, just use the `CGPDFDocumentCreateWithURL()` and `CGPDFDocumentGetPage()` functions directly.
0xced
I'd rather not use undocumented methods, they are prone to disappearing... and I don't necessarily have a URL, because the PDF may not actually BE a file yet... Often I'm making the PDF straight out of scanned data.
Brian Postow
Well, the undocumented method would be for testing purpose only. The other way to officially get a CGPDFDocumentRef is to use the `CGPDFDocumentCreateWithProvider()` function.
0xced
A: 

Try feeding the data for the PDF document (as opposed to a single page) into the CGImageSource. Images from the source should correspond to pages in the document.

Peter Hosey
I don't think CGImageSource supports PDF. I just tried calling `CFShow( CGImageSourceCopyTypeIdentifiers() );` on Snow Leopard, and got a list of 39 UTIs, but com.adobe.pdf was not on the list.
JWWalker