把pdf页面绘制到ContextReference上,然后将ContextReference再转换成UIImage,以下为代码:
-(UIImage *)getUIImageFromPDFPage:(int)page_number pdfPage: (CGPDFPageRef)ppage
{
CGRect pageRect = CGPDFPageGetBoxRect(ppage, kCGPDFMediaBox);
pageRect.origin = CGPointZero;
//开启图片绘制 上下文
UIGraphicsBeginImageContext(pageRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
// 设置白色背景
CGContextSetRGBFillColor(context, 1.0,1.0,1.0,1.0);
CGContextFillRect(context,pageRect);
CGContextSaveGState(context);
//进行翻转
CGContextTranslateCTM(context, 0.0, pageRect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextConcatCTM(context, CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, pageRect, 0,true));
CGContextDrawPDFPage(context, page);
CGContextRestoreGState(context);
UIImage *pdfImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return pdfImage;
}