在 Cocoa 中将 PDF 转换为高分辨率图像的最佳方法

发布于 2024-12-08 10:36:28 字数 127 浏览 4 评论 0原文

在 Cocoa 中将 PDF 转换为 300 dpi(例如)tiff 的最佳方法是什么?

我使用 PDFImageRep 创建 NSImage 但我找不到放大分辨率的方法。

What is the best way to convert PDF to 300 dpi (for example) tiff in Cocoa?

I use PDFImageRep for creating NSImage but I cannot find the way to enlarge the resolution.

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

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

发布评论

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

评论(1

污味仙女 2024-12-15 10:36:28

解决方案是为图像创建图形上下文并在该上下文中渲染 PDF 页面。创建上下文时,您指定所需的分辨率。
此代码将完成这项工作:

+ (UIImage *) convertPDFPageToImage: (CGPDFPageRef) page withResolution: (float) resolution {   
    CGRect cropBox = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
    int pageRotation = CGPDFPageGetRotationAngle(page);

    if ((pageRotation == 0) || (pageRotation == 180) ||(pageRotation == -180)) {
        UIGraphicsBeginImageContextWithOptions(cropBox.size, NO, resolution / 72); 
    }
    else {
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(cropBox.size.height, cropBox.size.width), NO, resolution / 72); 
    }

    CGContextRef imageContext = UIGraphicsGetCurrentContext();   

    [PDFPageRenderer renderPage:page inContext:imageContext];

    UIImage *pageImage = UIGraphicsGetImageFromCurrentImageContext();   

    UIGraphicsEndImageContext();

    return pageImage;
}

它取自此处: http://ipdfdev.com/2011/03/28/convert-a-pdf-page-to-image-on-the-iphone-and-ipad/

The solution is to create a graphics context for your image and render the PDF page in that context. When you create you context you specify the desired resolution.
This code will do the job:

+ (UIImage *) convertPDFPageToImage: (CGPDFPageRef) page withResolution: (float) resolution {   
    CGRect cropBox = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
    int pageRotation = CGPDFPageGetRotationAngle(page);

    if ((pageRotation == 0) || (pageRotation == 180) ||(pageRotation == -180)) {
        UIGraphicsBeginImageContextWithOptions(cropBox.size, NO, resolution / 72); 
    }
    else {
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(cropBox.size.height, cropBox.size.width), NO, resolution / 72); 
    }

    CGContextRef imageContext = UIGraphicsGetCurrentContext();   

    [PDFPageRenderer renderPage:page inContext:imageContext];

    UIImage *pageImage = UIGraphicsGetImageFromCurrentImageContext();   

    UIGraphicsEndImageContext();

    return pageImage;
}

It is taken from here: http://ipdfdev.com/2011/03/28/convert-a-pdf-page-to-image-on-the-iphone-and-ipad/

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