在Cocoa中将PDF转换为高分辨率图像的最佳方式 [英] Best way to convert PDF to high-resolution image in Cocoa

查看:264
本文介绍了在Cocoa中将PDF转换为高分辨率图像的最佳方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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

推荐答案

解决方案是为您的图像创建一个图形上下文,并在该上下文中呈现PDF页面。当您创建上下文时,指定所需的分辨率。
此代码将完成此任务:

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

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

这篇关于在Cocoa中将PDF转换为高分辨率图像的最佳方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆