我在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];
}
非常感谢!
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