Drawing a PDF file on the right side in a CGContext

I override the drawRect: method in a custom UIView, and I am doing some custom drawing. Everything went well until I had to draw a PDF resource (more precisely, a vector glyph) into context. First I extract the PDF from the 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 window with the same dimensions as the downloaded PDF:

CGRect box = CGPDFPageGetBoxRect(pdfPage, kCGPDFArtBox);

Then I save my graphic state so that I don't blame anything:

CGContextSaveGState(context);

And then I do a CTM scale + translation, theoretically flipping the whole context:

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

Then I scale the PDF so that it fits the view correctly:

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

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

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

The problem is that nothing is visible. All this code should theoretically draw a PDF glyphate in the view, right?

+ , , : .

?

+3
4

:

CGContextTranslateCTM(context, 0.0, rect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
+10
CGContextTranslateCTM(context, 0, self.bounds.size.height);
CGContextScaleCTM(context, 1.0, -1.0);

CGPDFPageRef page = CGPDFDocumentGetPage(pdf, currentPage);
CGContextSaveGState(context);




CGRect box = CGPDFPageGetBoxRect(page, kCGPDFMediaBox);

CGContextScaleCTM(context, self.bounds.size.width/box.size.width, self.bounds.size.height/box.size.height);
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);

+2

To scale up the translation, I think we can put a minus on the translation.

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

This will turn everything right side for me:

pdfDisplayView.layer.geometryFlipped = YES; //(NO) 
0
source

Source: https://habr.com/ru/post/1737269/


All Articles