将PDF转换为PNG

时间:2013-11-23 13:48:12

标签: iphone image pdf

我会将有时超过一页的PDF转换为PNG。

实际上我使用此代码:

NSURL* url = [NSURL fileURLWithPath: pdfPath];

CGPDFDocumentRef document = CGPDFDocumentCreateWithURL ((CFURLRef) url);

UIGraphicsBeginImageContext(CGSizeMake(596,842));
CGContextRef currentContext = UIGraphicsGetCurrentContext();

CGContextTranslateCTM(currentContext, 0, 842);
CGContextScaleCTM(currentContext, 1.0, -1.0); // make sure the page is the right way up

CGPDFPageRef page = CGPDFDocumentGetPage (document, 1); // first page of PDF is page 1 (not zero)
CGContextDrawPDFPage (currentContext, page);  // draws the page in the graphics context

UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSString* imagePath = [localDocuments stringByAppendingPathComponent: @"Karte.png"];
[UIImagePNGRepresentation(image) writeToFile: imagePath atomically:YES];

但这只转换了第一页。有任何一个想法如何将所有页面转换为一个png?

0 个答案:

没有答案