将 CGPDFPage 渲染为 UIImage

发布于 2024-10-11 06:17:08 字数 1084 浏览 2 评论 0原文

我正在尝试将 CGPDFPage (从 CGPDFDocument 中选择)渲染到 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 的一部分,但从后到前且上下颠倒地旋转。我的问题是,如何选择完整的 pdf 页面并将其翻转以正确显示。我看过一些使用 ScaleCTM 和 TranslateCTM 的示例,但似乎无法让它们工作。

ObjectiveC 中的任何示例都可以,我会接受我能得到的所有帮助:)

谢谢

I'm trying to render a CGPDFPage (selected from a CGPDFDocument) into a UIImage to display on a view.

I have the following code in MonoTouch which gets me part way there.

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);
    }

A section of the CGPDFPage is displayed but rotated back-to-front and upside down. My question is, how do I select the full pdf page and flip it round to display correctly. I have seen a few examples using ScaleCTM and TranslateCTM but couldn't seem to get them working.

Any examples in ObjectiveC are fine, I'll take all the help I can get :)

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜吻♂芭芘 2024-10-18 06:17:08

我没有使用过 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;

}

I haven't worked with MonoTouch. However, in objective-C you would get an image for a PDF page like this (notice the CTM transforms):

-(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;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文