views:

341

answers:

1

I'm overriding the drawRect: method in a custom UIView, and I'm doing some custom drawing. All was going well, until I needed to draw a PDF resource (a vector glyph, to be precise) into the context. First I retrieve the PDF from a file:

NSURL *pdfURL = [NSURL fileURLWithPath:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"CardKit.bundle/A.pdf"]];
CGPDFDocumentRef pdfDoc = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);
CGPDFPageRef pdfPage = CGPDFDocumentGetPage(pdfDoc, 1);

Then I create a box with the same dimensions as the loaded PDF:

CGRect box = CGPDFPageGetBoxRect(pdfPage, kCGPDFArtBox);

Then I save my graphics state, so that I don't screw anything up:

CGContextSaveGState(context);

And then I perform a scale+translate of the CTM, theoretically flipping the whole context:

CGContextScaleCTM(context, 1.0, -1.0);
CGContextTranslateCTM(context, 0.0, rect.size.height);

I then scale the PDF so that it fits into the view properly:

CGContextScaleCTM(context, rect.size.width/box.size.width, rect.size.height/box.size.height);

And finally, I draw the PDF and restore the graphics state:

CGContextDrawPDFPage(context, pdfPage);
CGContextRestoreGState(context);

The issue is that there is nothing visible drawn. All this code should theoretically draw the PDF glyph into the view, right?

If I remove the scale+translate used to flip the context, it draws perfectly: it just draws upside-down.

Any ideas?

+3  A: 

Try doing the translate before the scale:

CGContextTranslateCTM(context, 0.0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
Rob Keniger
This produces the exact same result, I'm afraid.
Carter Allen
Carter Allen: The code as I have corrected it should work just fine. Translating and then scaling is exactly what you need to do to flip (or, in this case, unflip) the co-ordinate system.
Peter Hosey
Yes, this code is what I use for flipping Quartz drawing in some of my applications, so it should work.
Brad Larson
Again: I'm using that exact code. It results in an empty view. I'm not sure what's going wrong, but without those two lines, it draws correctly, just upside-down.
Carter Allen
The code as Peter corrected it (thanks, BTW, I forgot to swap the lines!) definitely works here. Can you post the entire code of your `drawRect:` method?
Rob Keniger
Here is the drawRect: code in its entirety: http://zcr.me/us
Carter Allen
Your code as posted works immediately for me.
Rob Keniger
The code as posted should draw the PDF source file, right-side-up, with a white gradient background. Is that what is happening?
Carter Allen
Yes, it works fine for me.
Rob Keniger
I have no idea what I'm doing differently here. Is there anything special that you've configured in IB, or in the view class, or anything?I don't know if it is related, but this is inside a library which is getting loaded into another app: but that shouldn't change anything.
Carter Allen
This might have something to do with it. Here is the PDF I am using, try using it: http://zcr.me/f/A.pdf
Carter Allen
Interesting, your PDF fails as you describe.
Rob Keniger
Does anyone know what might be causing that? The PDF seems fairly standard as glyphs go. I don't know what's wrong...
Carter Allen
Carter Allen: You're not releasing your CGGradient or CGColorSpace objects. `rect` is the rect you need to draw; it isn't necessarily the whole bounds of the view, so you might be drawing the page into too small of a rectangle for you to see it. Don't forget to take the page's art box origin into account. (Also, are you sure you don't want the media box or crop box?) Have you verified that `pdfPage` is not `NULL`?
Peter Hosey