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

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

问题描述

我们目前正在使用 UIWebViews 显示PDF内容。理想情况下,我想能够在 UITableView 中显示缩略图,而不会同时加载许多不同的 UIWebViews 没有任何人有关于我如何做这方面的任何提示?






$ b

我已经考虑过屏幕截图使用 UIDocumentInteractionController UIWebView 加载的文档,

解决方案

苹果提供了一大堆方法,在 CoreGraphics 级别用于直接绘制PDF内容。据我所知,没有一个整齐地包装在 UIKit 级别,因此它可能不是一个很好的适合您的项目在这一点,特别是如果你在C级别不怎么舒服。但是,相关的函数是 CGContextDrawPDFPage; 每正常 CoreGraphics 方式的东西有其他方法来创建PDF参考从数据源,然后从PDF获取页面引用。然后你需要处理缩放和翻译到你想要的视图,并警告你需要执行水平翻转,因为PDF(和OS X)使用左下角作为原点,而iOS使用左上角。示例代码,我的头顶:

  UIGraphicsBeginImageContext(thumbnailSize); 
CGPDFDocumentRef pdfRef = CGPDFDocumentCreateWithProvider((CGDataProviderRef)instanceOfNSDataWithPDFInside);
CGPDFPageRef pageRef = CGPDFDocumentGetPage(pdfRef,1); //获取第一页

CGContextRef contextRef = UIGraphicsGetCurrentContext();

//忽略这里的转换问题;取决于你想要什么和
//涉及一堆正常的CoreGraphics的东西,是无关的
//与PDFs
CGContextDrawPDFPage(contextRef,pageRef);

UIImage * imageToReturn = UIGraphicsGetImageFromCurrentImageContext();

//清理
UIGraphicsEndImageContext();
CGPDFDocumentRelease(pdfRef);

return imageToReturn;

猜猜,你可能想使用 CGPDFPageGetBoxRect kCGPDFCropBox)以获取页面的裁剪框,然后找出如何缩放/移动以适应您的图片大小。



目的是 CALayer 上的 -renderInContext:方法(见 QA 1703 ) - 获取屏幕截图的旧手段是 UIGetScreenImage ,但这从来没有真正的官方API,并且似乎临时允许只是因为意外批准RedLaser。有了QA中的代码,你可以钻取自己的UIImage从任何其他视图,而不必视图必须在屏幕上。哪些可能解决了您的屏幕捕获的一些问题?虽然这意味着您只能支持OS 4.x。



在任何一种情况下,PDF都不能快速绘制。您可能需要填充表,然后在后台线程上绘制缩略图,如果可用,将其向上推。你不能在后台线程上安全地使用UIKit对象,但所有 CoreGraphics 的东西都是安全的。


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+!

Does anyone have any tips on how I go about doing this?

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 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;

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.

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.

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.

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

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