开发者

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

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

I use PDFImageRep for creating NSImage but I c开发者_运维百科annot find the way to enlarge the resolution.


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/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜