将PDF转换为UIImageView [英] Convert PDF to UIImageView

查看:121
本文介绍了将PDF转换为UIImageView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现了一些代码,它从PDF文件中提供了 UIImage 。它有效,但我有两个问题:

I've found some code which gives me a UIImage out of a PDF-File. It works, but I have two questions:


  • 是否有可能获得更好的UIImage质量? (见截图)

  • 我只看到 UIImageView 中的第一页。我是否必须将文件嵌入 UIScrollView 才能完成?

  • 或者只渲染一个页面并使用按钮更好浏览页面?

  • Is there a possibility to achieve a better quality of the UIImage? (See Screenshot)
  • I only see the first page in my UIImageView. Do I have to embed the file in a UIScrollView to be complete?
  • Or is it better to render just one page and use buttons to navigate through the pages?

PS我知道 UIWebView 可以显示具有一些功能的PDF-Pages但我需要它作为 UIImage 或者至少在的UIView

P.S. I know that UIWebView can display PDF-Pages with some functionalities but I need it as a UIImage or at least in a UIView.

质量不好图片:

代码:

-(UIImage *)image {
UIGraphicsBeginImageContext(CGSizeMake(280, 320)); 

CGContextRef context = UIGraphicsGetCurrentContext();

CFURLRef pdfURL = CFBundleCopyResourceURL(CFBundleGetMainBundle(), CFSTR("ls.pdf"), NULL, NULL);

CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfURL);

CGContextTranslateCTM(context, 0.0, 320);

CGContextScaleCTM(context, 1.0, -1.0);

CGPDFPageRef page = CGPDFDocumentGetPage(pdf, 4);

CGContextSaveGState(context);

CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFCropBox, CGRectMake(0, 0, 280, 320), 0, true);

CGContextConcatCTM(context, pdfTransform);

CGContextDrawPDFPage(context, page);

CGContextRestoreGState(context);

UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();  
UIGraphicsEndImageContext();
return resultingImage;
}


推荐答案

你在做什么 CGContextTranslateCTM(context,0.0,320); call?

What are you doing with the CGContextTranslateCTM(context, 0.0, 320); call?

您应该从pdf中提取适当的指标,像这样的代码:

You should extract the proper metrics form the pdf, with code like this:

 cropBox = CGPDFPageGetBoxRect(page, kCGPDFCropBox);
 rotate = CGPDFPageGetRotationAngle(page);

另外,如你所见,pdf可能有旋转信息,所以你需要使用 CGContextTranslateCTM / CGContextRotateCTM / CGContextScaleCTM 取决于角度。

Also, as you see, the pdf might has rotation info, so you need to use the CGContextTranslateCTM/CGContextRotateCTM/CGContextScaleCTM depending on the angle.

您还可能想要剪辑<$以外的任何内容c $ c> CropBox 区域,因为pdf有各种 viewPorts ,你通常不想显示(例如对于打印机,以便可以进行无缝打印) ) - >使用 CGContextClip

You also might wanna clip any content that is outside of the CropBox area, as pdf has various viewPorts that you usually don't wanna display (e.g. for printers so that seamless printing is possible) -> use CGContextClip.

接下来,您忘记了pdf参考定义了白色背景颜色。有很多文档根本没有定义任何背景颜色 - 如果你不自己绘制白色背景你会得到奇怪的结果 - > CGContextSetRGBFillColor & CGContextFillRect

Next, you're forgetting that the pdf reference defines a white background color. There are a lot of documents out there that don't define any background color at all - you'll get weird results if you don't draw a white background on your own --> CGContextSetRGBFillColor & CGContextFillRect.

这篇关于将PDF转换为UIImageView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆