问题
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