Reputation: 1293
Is there a way to save PDF pages to images on OS X by Xcode 5? I have searched on Stackoverflow and found this Xcode save a PDF as Image?
However it is for iOS, not OS X. I want to export PDF pages to images like JPG or PNG etc.
Thank you in advance.
Upvotes: 0
Views: 1117
Reputation: 2918
Try this:
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];
}
Upvotes: 1