COCOA中的PDF到JPG转换

4 pdf cocoa image

在我的cocoa应用程序中,我需要将pdf的每个页面保存为jpg ..如何使用Objective C进行处理

Pet*_*sey 5

使用纯Objective-C(Cocoa和PDF Kit),迭代PDF文档的页面,并为每个页面创建一个NSImage,其大小是页面媒体框的大小,锁定焦点,告诉页面绘制,使用聚焦视图(图像)创建NSBitmapImageRep,解锁图像焦点,然后向位图图像代表询问JPEG数据并将该数据写入文件. (这个解决方案很糟糕;不要使用它.)

另一种方法是Core Graphics和ImageIO.创建与文档的媒体框和颜色空间匹配的位图上下文,然后为每个页面创建JPEG文件的图像目标,从文档中获取页面,在上下文中绘制页面,从上下文创建CGImage,使用清除上下文CGContextClearRect,将图像添加到目标,并最终确定目标.

  • NSPDFImageRep *img = [NSPDFImageRep imageRepWithContentsOfFile:path]; 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]]; [fileMangr createFileAtPath:[NSString stringWithFormat:@"%@/%@", foldr, pageName] contents:finalData 属性:nil];} (3认同)