Created
January 9, 2011 15:38
-
-
Save jamesantrobus/771759 to your computer and use it in GitHub Desktop.
Rough demo to transform a CGPDFPage to a UIImage with a given width
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| private UIImage TransformToImage(CGPDFPage page, Single width) | |
| { | |
| RectangleF pageRect = page.GetBoxRect(CGPDFBox.Media);; | |
| Single pdfScale = width / pageRect.Size.Width; | |
| pageRect.Size = new SizeF(pageRect.Size.Width*pdfScale, pageRect.Size.Height*pdfScale); | |
| //pageRect.Origin = CGPointZero; | |
| UIGraphics.BeginImageContext(pageRect.Size); | |
| CGContext context = UIGraphics.GetCurrentContext(); | |
| //White BG | |
| context.SetRGBFillColor(1.0f,1.0f,1.0f,1.0f); | |
| context.FillRect(pageRect); | |
| context.SaveState(); | |
| // Next 3 lines makes the rotations so that the page look in the right direction | |
| context.TranslateCTM(0.0f, pageRect.Size.Height); | |
| context.ScaleCTM(1.0f, -1.0f); | |
| CGAffineTransform transform = page.GetDrawingTransform(CGPDFBox.Media, pageRect, 0, true); | |
| context.ConcatCTM(transform); | |
| context.DrawPDFPage(page); | |
| context.RestoreState(); | |
| UIImage img = UIGraphics.GetImageFromCurrentImageContext(); | |
| UIGraphics.EndImageContext(); | |
| return img; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment