如何使用iOS以编程方式生成PDF的缩略图? [英] How can I programmatically generate a thumbnail of a PDF with iOS?

查看:272
本文介绍了如何使用iOS以编程方式生成PDF的缩略图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我们正在使用UIWebViews显示PDF内容.理想情况下,我希望能够在UITableView中显示缩略图,而无需同时加载许多不同的UIWebViews ...它们加载一个文档的速度足够慢-没关系10 +!

We're displaying PDF content using UIWebViews at the moment. Ideally I would like to be able to display thumbnails in the UITableView without loading many different UIWebViews concurrently... they're slow enough loading one document - never mind 10+!

我该怎么做?

我已经考虑过用屏幕捕获使用UIDocumentInteractionControllerUIWebView加载的文档,但这意味着在显示表格之前都必须将它们全部缩略图化.

I've thought about screen capturing a document loaded using UIDocumentInteractionController or UIWebView but this means they'd all have to be thumbnailed before displaying the table.

推荐答案

Apple在CoreGraphics级别提供了大量方法来直接绘制PDF内容.据我所知,它们都没有在UIKit级别上整齐地打包,因此这可能不适合您的项目,特别是如果您不太满意C级别的话.但是,按照正常的CoreGraphics方式,相关功能是CGContextDrawPDFPage;,还有其他方法可以从数据源创建PDF参考,然后从PDF获取页面参考.然后,您需要处理缩放并转换为所需的视图,并被警告您需要执行水平翻转,因为PDF(和OS X)使用左下方作为原点,而iOS使用左上方.示例代码,浮出水面:

Apple supply a whole bunch of methods down at the CoreGraphics level for drawing PDF content directly. As far as I'm aware, none of it is neatly packaged up at the UIKit level, so it may not be a good fit for your project at this point, especially if you're not as comfortable down at the C level. However, the relevant function is CGContextDrawPDFPage; per the normal CoreGraphics way of things there are other methods to create a PDF reference from a data source and then to get a page reference from a PDF. You'll then need to deal with scaling and translating to the view you want, and be warned that you'll need to perform a horizontal flip because PDFs (and OS X) use the lower left as the origin whereas iOS uses the top left. Example code, off the top of my head:

UIGraphicsBeginImageContext(thumbnailSize);
CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithProvider( (CGDataProviderRef)instanceOfNSDataWithPDFInside );
CGPDFPageRef pageRef = CGPDFDocumentGetPage(pdfRef, 1); // get the first page

CGContextRef contextRef = UIGraphicsGetCurrentContext();

// ignore issues of transforming here; depends on exactly what you want and
// involves a whole bunch of normal CoreGraphics stuff that is nothing to do
// with PDFs
CGContextDrawPDFPage(contextRef, pageRef);

UIImage *imageToReturn = UIGraphicsGetImageFromCurrentImageContext();

// clean up
UIGraphicsEndImageContext();
CGPDFDocumentRelease(pdfRef);

return imageToReturn;

一个猜测,您可能会想使用CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox)来获取页面的裁剪框,然后找出如何缩放/移动该裁剪框以适合您的图像大小.

At a guess, you'll probably want to use CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox) to get the page's crop box and then work out how to scale/move that to fit your image size.

对于您来说,可能更容易使用的是CALayer上的-renderInContext:方法(请参见质量检查1703 )-获取屏幕截图的旧方法是UIGetScreenImage,但这并不是真正的官方API,而且似乎只是由于RedLaser的意外批准而暂时被允许.借助质量检查中的代码,您可以自行安装以从任何其他视图获取UIImage,而无需将该视图显示在屏幕上.哪个可以解决您的屏幕捕获问题?虽然这意味着您只能支持OS 4.x.

Probably easier for your purposes is the -renderInContext: method on CALayer (see QA 1703) — the old means of getting a screenshot was UIGetScreenImage, but that was never really official API and was seemingly temporarily allowed only because of the accidental approval of RedLaser. With the code in the QA you can rig yourself up to get a UIImage from any other view without that view having to be on screen. Which possibly resolves some of your issue with screen capturing? Though it means you can support OS 4.x only.

无论哪种情况,PDF都不会很快绘制出来.您可能需要填充表格,然后在后台线程上绘制缩略图,并在可用时将其向上推.您实际上不能在后台线程上安全地使用UIKit对象,但是所有CoreGraphics内容都是安全的.

In either case, PDFs just don't draw that quickly. You probably need to populate the table then draw the thumbnails on a background thread, pushing them upwards when available. You can't actually use UIKit objects safely on background threads but all the CoreGraphics stuff is safe.

这篇关于如何使用iOS以编程方式生成PDF的缩略图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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