Best way to convert PDF to high-resolution image in Cocoa

末鹿安然 提交于 2019-12-11 05:39:37

问题


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.


回答1:


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/



来源:https://stackoverflow.com/questions/7661479/best-way-to-convert-pdf-to-high-resolution-image-in-cocoa

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!