如何将PDF转换为NSImage并更改DPI?

时间:2014-05-13 08:44:04

标签: ios xcode macos pdf nsimage

我尝试将PDF页面转换为NSImage并成功保存到JPG文件。但是输出结果与正常的72 DPI相同。我想将DPI更改为300 DPI但失败了。以下是代码:

- (IBAction)TestButton:(id)sender {

    NSString* localDocuments = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
    NSString* pdfPath = [localDocuments stringByAppendingPathComponent:@"1.pdf"];

    NSData *pdfData = [NSData dataWithContentsOfFile:pdfPath];
    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];

        CGFloat factor = 300/72; // Scale from 72 DPI to 300 DPI
        //NSImage *img; // Source image
        NSSize newSize = NSMakeSize(temp.size.width*factor, temp.size.height*factor);
        NSImage *scaledImg = [[NSImage alloc] initWithSize:newSize];
        [scaledImg lockFocus];
        [[NSColor whiteColor] set];
        [NSBezierPath fillRect:NSMakeRect(0, 0, newSize.width, newSize.height)];
        NSAffineTransform *transform = [NSAffineTransform transform];
        [transform scaleBy:factor];
        [transform concat];
        [temp drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
        [scaledImg unlockFocus];


        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:@"%@%@", pdfPath, pageName] contents:finalData attributes:nil];
    }
}

2 个答案:

答案 0 :(得分:3)

您可以通过将NSImageRep的大小设置为图像大小以外的其他值来设置保存在图像文件元数据中的分辨率

[pngImageRep setSize:NSMakeSize(targetWith, targetHeight)]

您必须将targetWidth和targetHeight初始化为您想要的值

编辑:我想你想写&#34; scaledImg&#34;不是&#34; temp&#34;

NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[scaledImg TIFFRepresentation]];

编辑2:第二个想法这会让你得到一个更大的图像,但只作为较小图像的延伸版本。 weichsel的方法回答下面的修改可能就是你真正想要的(但上面的代码对于设置元数据仍然有效)

NSSize newSize = NSMakeSize(pdfImageRep.size.width * factor,pdfImageRep.size.height * factor);
NSImage* scaledImage = [NSImage imageWithSize:newSize flipped:NO drawingHandler:^BOOL(NSRect dstRect) {
    [pdfImageRep drawInRect:dstRect];
    return YES;
}];

答案 1 :(得分:2)

从OS X 10.8开始,NSImage has a block based initialiser将基于矢量的内容绘制到位图中 我们的想法是提供一个绘图处理程序,只要请求图像的表示就会调用它 点和像素之间的关系通过将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 defined in terms of points.
     * By explicitly setting the size of the scaled representation in Pixels, you 
     * define the relation between points & 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];
}