Macos 如何将PDF转换为CGImage,然后保存为高DPI JPG图像?

Macos 如何将PDF转换为CGImage,然后保存为高DPI JPG图像?,macos,pdf,cgimage,Macos,Pdf,Cgimage,我知道有一种方法可以将PDF页面保存到NSImage,然后输出到JPG,如下所示: NSData *pdfData = [NSData dataWithContentsOfFile:pathToUrPDF]; NSPDFImageRep *pdfImg = [NSPDFImageRep imageRepWithData:pdfData]; NSFileManager *fileManager = [NSFileManager defaultManager]; NSInteger page

我知道有一种方法可以将PDF页面保存到
NSImage
,然后输出到JPG,如下所示:

NSData *pdfData = [NSData dataWithContentsOfFile:pathToUrPDF];    
NSPDFImageRep *pdfImg = [NSPDFImageRep imageRepWithData:pdfData];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSInteger pageCount = [pdfImg pageCount];
for(int i = 0 ; i < pageCount ; i++) {
    [pdfImg setCurrentPage:i];
    NSImage *temp = [[NSImage alloc] init];
    [temp addRepresentation:pdfImg];
    NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[temp TIFFRepresentation]];
    NSData *finalData = [rep representationUsingType:NSJPEGFileType properties:nil];
    NSString *pageName = [NSString stringWithFormat:@"Page_%ld.jpg", (long)[pdfImg currentPage]];
    [fileManager createFileAtPath:[NSString stringWithFormat:@"%@/%@", @"pathWrUWantToSave", pageName] contents:finalData attributes:nil];
}
NSData*pdfData=[NSData dataWithContentsOfFile:pathToUrPDF];
NSPDIMAGEREP*pdfImg=[NSPDIMAGEREP IMAGEREP WITHDATA:pdfData];
NSFileManager*fileManager=[NSFileManager defaultManager];
NSInteger pageCount=[pdfImg pageCount];
for(int i=0;i
但是“
tiffrepresentation
”只能输出为最高72 DPI。因此,我认为从PDF中获取高DPI图像的最佳方法是在Mac OS X上使用
CGImage
。如何做到这一点?我的目标应用程序是Mac OS X,而不是iOS


非常感谢

我终于找到了解决方案:

自OSX10.8以来,NSImage有一个基于块的初始化器,用于将基于向量的内容绘制到位图中。 其思想是提供一个图形处理程序,每当请求图像的表示时都会调用该处理程序。 点和像素之间的关系通过向初始化器传递NSSize(以点为单位)来表示,并显式设置表示的像素尺寸:

NSString* localDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
NSString* pdfPath = [localDocuments stringByAppendingPathComponent:@"1.pdf"];
NSData* pdfData = [NSData dataWithContentsOfFile:pdfPath];
NSPDFImageRep* pdfImageRep = [NSPDFImageRep imageRepWithData:pdfData];
CGFloat factor = 300/72;
NSInteger pageCount = [pdfImageRep pageCount];
for(int i = 0 ; i < pageCount ; i++) {
    [pdfImageRep setCurrentPage:i];
    NSImage* scaledImage = [NSImage imageWithSize:pdfImageRep.size flipped:NO drawingHandler:^BOOL(NSRect dstRect) {
        [pdfImageRep drawInRect:dstRect];
        return YES;
    }];
    NSImageRep* scaledImageRep = [[scaledImage representations] firstObject];
    /*
     * The sizes of the PDF Image Rep and the [NSImage  imageWithSize: drawingHandler:]-context
     * are define in terms of points.
     * By explicitly setting the size of the scaled representation in in Pixels, you 
     * define the relation between ponts & pixels.
     */
    scaledImageRep.pixelsWide = pdfImageRep.size.width * factor;
    scaledImageRep.pixelsHigh = pdfImageRep.size.height * factor;
    NSBitmapImageRep* pngImageRep = [NSBitmapImageRep imageRepWithData:[scaledImage TIFFRepresentation]];
    NSData* finalData = [pngImageRep representationUsingType:NSJPEGFileType properties:nil];
    NSString* pageName = [NSString stringWithFormat:@"Page_%ld.jpg", (long)[pdfImageRep currentPage]];
    [[NSFileManager defaultManager] createFileAtPath:[NSString stringWithFormat:@"%@%@", pdfPath, pageName] contents:finalData attributes:nil];
}
NSString*localDocuments=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];
NSString*pdfPath=[localDocuments stringByAppendingPathComponent:@“1.pdf”];
NSData*pdfData=[NSData DATA WITH CONTENTS OFFILE:pdfPath];
NSPDIMAGEREP*pdfImageRep=[NSPDIMAGEREP IMAGEREP WITHDATA:pdfData];
CGFloat系数=300/72;
NSInteger pageCount=[pdfImageRep pageCount];
for(int i=0;i
我终于找到了解决方案:

自OSX10.8以来,NSImage有一个基于块的初始化器,用于将基于向量的内容绘制到位图中。 其思想是提供一个图形处理程序,每当请求图像的表示时都会调用该处理程序。 点和像素之间的关系通过向初始化器传递NSSize(以点为单位)来表示,并显式设置表示的像素尺寸:

NSString* localDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
NSString* pdfPath = [localDocuments stringByAppendingPathComponent:@"1.pdf"];
NSData* pdfData = [NSData dataWithContentsOfFile:pdfPath];
NSPDFImageRep* pdfImageRep = [NSPDFImageRep imageRepWithData:pdfData];
CGFloat factor = 300/72;
NSInteger pageCount = [pdfImageRep pageCount];
for(int i = 0 ; i < pageCount ; i++) {
    [pdfImageRep setCurrentPage:i];
    NSImage* scaledImage = [NSImage imageWithSize:pdfImageRep.size flipped:NO drawingHandler:^BOOL(NSRect dstRect) {
        [pdfImageRep drawInRect:dstRect];
        return YES;
    }];
    NSImageRep* scaledImageRep = [[scaledImage representations] firstObject];
    /*
     * The sizes of the PDF Image Rep and the [NSImage  imageWithSize: drawingHandler:]-context
     * are define in terms of points.
     * By explicitly setting the size of the scaled representation in in Pixels, you 
     * define the relation between ponts & pixels.
     */
    scaledImageRep.pixelsWide = pdfImageRep.size.width * factor;
    scaledImageRep.pixelsHigh = pdfImageRep.size.height * factor;
    NSBitmapImageRep* pngImageRep = [NSBitmapImageRep imageRepWithData:[scaledImage TIFFRepresentation]];
    NSData* finalData = [pngImageRep representationUsingType:NSJPEGFileType properties:nil];
    NSString* pageName = [NSString stringWithFormat:@"Page_%ld.jpg", (long)[pdfImageRep currentPage]];
    [[NSFileManager defaultManager] createFileAtPath:[NSString stringWithFormat:@"%@%@", pdfPath, pageName] contents:finalData attributes:nil];
}
NSString*localDocuments=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)objectAtIndex:0];
NSString*pdfPath=[localDocuments stringByAppendingPathComponent:@“1.pdf”];
NSData*pdfData=[NSData DATA WITH CONTENTS OFFILE:pdfPath];
NSPDIMAGEREP*pdfImageRep=[NSPDIMAGEREP IMAGEREP WITHDATA:pdfData];
CGFloat系数=300/72;
NSInteger pageCount=[pdfImageRep pageCount];
for(int i=0;i
我使用PDF来管理与分辨率无关的图像,这些图像可以根据需要进行缩放。在块中绘图有一个非常好的副作用,那就是大大加快了PDF光栅yosemite的速度。我使用PDF来管理与分辨率无关的图像,这些图像可以缩放以适应所需的任何大小。在块中绘图有一个非常好的副作用,那就是大大加快了PDF光栅的速度,约塞米蒂。