使用COCOA将PDF页面转换为图像

3

我在PDF转换成图片方面遇到了问题。我希望为PDF文档中的每一页创建一个图像文件。

这是我正在使用的代码,它可以正常工作。每一页都会被转换为图像,但我在图像分辨率方面遇到了问题。我不知道如何设置输出图像的分辨率。有谁能帮帮我吗?

NSData *pdfData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://localhost/test/test.pdf"]];


NSPDFImageRep *img = [NSPDFImageRep imageRepWithData:pdfData]; 
 NSFileManager *fileManager = [NSFileManager defaultManager];
 int count = [img pageCount]; 
 for(int i = 0 ; i < count ; i++) { 
  [img setCurrentPage:i]; 
  NSImage *temp = [[NSImage alloc] init]; 
  [temp addRepresentation:img]; 
  NSBitmapImageRep *rep = [NSBitmapImageRep imageRepWithData:[temp TIFFRepresentation]]; 
  NSData *finalData = [rep representationUsingType:NSJPEGFileType properties:nil]; 
  NSString *pageName = [NSString stringWithFormat:@"Page_%d.jpg", [img currentPage]]; 
  [fileManager createFileAtPath:[NSString stringWithFormat:@"%@/%@", @"/Users/mac/Desktop/", pageName] contents:finalData attributes:nil];
 }

非常感谢!
2个回答

2

我不认为那样做会奏效,因为我需要调用 [NSPDFImageRep imageRepWithData:pdfData] 方法,该方法创建并返回一个使用指定 PDF 数据初始化的 NSPDFImageRep 对象。如果我使用 [NSImageRep drawInRect:] 方法,如何获取要绘制到此矩形中的 PDF 页面呢? - Primoz Rome
1
对我来说,这种方法看起来运行良好。这是代码: NSRect bounds = pdfImageRep.bounds; CGFloat factor = 4.; NSRect rect = NSMakeRect(0, 0, bounds.size.width * factor, bounds.size.height * factor); NSImage *image = [[NSImage alloc] initWithSize:rect.size]; [image lockFocus]; [pdfImageRep drawInRect:rect]; [image unlockFocus]; - Holtwick

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接