将CGPDFPage渲染成UIImage

我想呈现一个CGPDFPage(从CGPDFDocumentselect)到UIImage显示在视图上。

我在MonoTouch中有下面的代码,它让我在那里的一部分。

RectangleF PDFRectangle = new RectangleF(0, 0, UIScreen.MainScreen.Bounds.Width, UIScreen.MainScreen.Bounds.Height); public override void ViewDidLoad () { UIGraphics.BeginImageContext(new SizeF(PDFRectangle.Width, PDFRectangle.Height)); CGContext context = UIGraphics.GetCurrentContext(); context.SaveState(); CGPDFDocument pdfDoc = CGPDFDocument.FromFile("test.pdf"); CGPDFPage pdfPage = pdfDoc.GetPage(1); context.DrawPDFPage(pdfPage); UIImage testImage = UIGraphics.GetImageFromCurrentImageContext(); pdfDoc.Dispose(); context.RestoreState(); UIImageView imageView = new UIImageView(testImage); UIGraphics.EndImageContext(); View.AddSubview(imageView); } 

显示CGPDFPage的一部分,但是反转和前后颠倒。 我的问题是,如何select完整的PDF页面,并翻转显示正确。 我已经看到一些使用ScaleCTM和TranslateCTM的例子,但似乎无法让它们工作。

在ObjectiveC中的任何例子都很好,我会尽我所能的帮助:)

谢谢

我没有使用MonoTouch。 但是,在Objective-C中,您将获得像这样的PDF页面的图像(注意CTM转换):

 -(UIImage *)getThumbForPage:(int)page_number{ CGFloat width = 60.0; // Get the page CGPDFPageRef myPageRef = CGPDFDocumentGetPage(myDocumentRef, page); // Changed this line for the line above which is a generic line //CGPDFPageRef page = [self getPage:page_number]; CGRect pageRect = CGPDFPageGetBoxRect(page, kCGPDFMediaBox); CGFloat pdfScale = width/pageRect.size.width; pageRect.size = CGSizeMake(pageRect.size.width*pdfScale, pageRect.size.height*pdfScale); pageRect.origin = CGPointZero; UIGraphicsBeginImageContext(pageRect.size); CGContextRef context = UIGraphicsGetCurrentContext(); // White BG CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0); CGContextFillRect(context,pageRect); CGContextSaveGState(context); // *********** // Next 3 lines makes the rotations so that the page look in the right direction // *********** CGContextTranslateCTM(context, 0.0, pageRect.size.height); CGContextScaleCTM(context, 1.0, -1.0); CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, pageRect, 0, true)); CGContextDrawPDFPage(context, page); CGContextRestoreGState(context); UIImage *thm = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return thm; }