文档操作:将pdf页面转换成UIImage
2016-03-21 17:52
525 查看
把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;
}
-(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;
}
相关文章推荐
- UE4的MaterialInstance作用
- 【笔记】《C#大学教程》- 第12章 GUI(二)Part2
- UIDynamic之物理特性
- UIDynamic之推行为
- UIDynamic之附着行为
- UIDynamic之甩(捕捉)行为
- UIDynamic之碰撞代理(监听碰撞行为)
- UIDynamic之重力+碰撞+边界
- UIDynamic之重力行为+碰撞行为
- UIDynamic之重力行为
- 核心动画与 UIView 封装的动画的区别
- 小结:UICollectionView 相关的属性和方法
- 练习:圆环效果(自定义 UICollectionViewFlowLayOut)
- 关于 UICollectionViewFlowLayout 的知识(含代理知识)
- 关于 UICollectionView 的一些代理和数据源方法
- 练习:UITableView 下做图片放大
- UINavigationController+UITabBarController
- 用 storyboard 创建 UITabBarController
- 代码创建 UITabBarController
- 用 storyboard 设置UINavigationController