【发布时间】:2014-04-11 03:36:26
【问题描述】:
有没有办法通过 Xcode 5 将 PDF 页面保存到 OS X 上的图像?我在 Stackoverflow 上搜索过,发现了这个Xcode save a PDF as Image?
但它适用于 iOS,而不是 OS X。我想将 PDF 页面导出为 JPG 或 PNG 等图像。
提前谢谢你。
【问题讨论】:
标签: objective-c macos pdf
有没有办法通过 Xcode 5 将 PDF 页面保存到 OS X 上的图像?我在 Stackoverflow 上搜索过,发现了这个Xcode save a PDF as Image?
但它适用于 iOS,而不是 OS X。我想将 PDF 页面导出为 JPG 或 PNG 等图像。
提前谢谢你。
【问题讨论】:
标签: objective-c macos pdf
试试这个:
NSData *pdfData = [NSData dataWithContentsOfFile:pathToUrPDF];
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];
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:@"%@/%@", @"pathWrUWantToSave", pageName] contents:finalData attributes:nil];
}
【讨论】: