使用COCOA将PDF页面转换为图像
我在将PDF转换为图像时遇到问题。我想为PDF文档中的每个页面创建一个图像文件 这是我正在使用的代码,运行良好。每个页面都会转换成图像,但我在图像分辨率方面有问题。我不知道如何设置输出图像的分辨率。有人能帮我吗使用COCOA将PDF页面转换为图像,cocoa,pdf,split,Cocoa,Pdf,Split,我在将PDF转换为图像时遇到问题。我想为PDF文档中的每个页面创建一个图像文件 这是我正在使用的代码,运行良好。每个页面都会转换成图像,但我在图像分辨率方面有问题。我不知道如何设置输出图像的分辨率。有人能帮我吗 NSData *pdfData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://localhost/test/test.pdf"]]; NSPDFImageRep *img = [NSPDFImageRep
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];
}
NSData*pdfData=[NSData dataWithContentsOfURL:[NSURL URLWithString:@]http://localhost/test/test.pdf"]];
NSPDIMAGEREP*img=[NSPDIMAGEREP IMAGEREP WITH DATA:pdfData];
NSFileManager*fileManager=[NSFileManager defaultManager];
int count=[img pageCount];
对于(int i=0;i
非常感谢 由于NSPDImageRep是NSImageRep的子类,您不能使用[NSImageRep drawInRect:]方法吗
链接:最简单的方法是使用ImageIO框架。将PDF数据馈送到以获取一个;将该对象馈送到以生成(并在同一步骤中选择性地保存)JPEG数据。在后一步中,可以在图像属性中指定分辨率;看
别忘了。这很重要。我认为这不起作用,因为我需要调用[NSPDIMAGEREP IMAGEREP WITHDATA:pdfData],它创建并返回一个使用指定PDF数据初始化的NSPDIMAGEREP对象。如果我使用[NSImageRep drawInRect:],我将如何让PDF页面绘制到这个矩形中?对我来说,这种方法似乎工作得很好。这是代码:NSRect bounds=pdfImageRep.bounds;CGFloat系数=4。;NSRect rect=NSMakeRect(0,0,bounds.size.width*因子,bounds.size.height*因子);NSImage*image=[[NSImage alloc]initWithSize:rect.size];[图像锁定焦点];[pdfImageRep drawInRect:rect];[图像解锁焦点];