Jam*_*man 11 pdf cocoa-touch objective-c ios
我们目前正在使用PDF内容UIWebViews.理想情况下,我希望能够在UITableView不加载许多不同的UIWebViews同时显示缩略图...它们足够慢以加载一个文档 - 更不用说10+!
有没有人对我如何做这个有任何提示?
我已经考虑过使用屏幕捕获加载的文档UIDocumentInteractionController,UIWebView但这意味着在显示表格之前必须先将它们缩略图.
alo*_*nes 13
这是考虑转换的示例代码.:)
NSURL* pdfFileUrl = [NSURL fileURLWithPath:finalPath];
CGPDFDocumentRef pdf = CGPDFDocumentCreateWithURL((CFURLRef)pdfFileUrl);
CGPDFPageRef page;
CGRect aRect = CGRectMake(0, 0, 70, 100); // thumbnail size
UIGraphicsBeginImageContext(aRect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
UIImage* thumbnailImage;
NSUInteger totalNum = CGPDFDocumentGetNumberOfPages(pdf);
for(int i = 0; i < totalNum; i++ ) {
CGContextSaveGState(context);
CGContextTranslateCTM(context, 0.0, aRect.size.height);
CGContextScaleCTM(context, 1.0, -1.0);
CGContextSetGrayFillColor(context, 1.0, 1.0);
CGContextFillRect(context, aRect);
// Grab the first PDF page
page = CGPDFDocumentGetPage(pdf, i + 1);
CGAffineTransform pdfTransform = CGPDFPageGetDrawingTransform(page, kCGPDFMediaBox, aRect, 0, true);
// And apply the transform.
CGContextConcatCTM(context, pdfTransform);
CGContextDrawPDFPage(context, page);
// Create the new UIImage from the context
thumbnailImage = UIGraphicsGetImageFromCurrentImageContext();
//Use thumbnailImage (e.g. drawing, saving it to a file, etc)
CGContextRestoreGState(context);
}
UIGraphicsEndImageContext();
CGPDFDocumentRelease(pdf);
Run Code Online (Sandbox Code Playgroud)
Tom*_*mmy 12
Apple提供了一大堆方法,CoreGraphics用于直接绘制PDF内容.据我所知,它没有一个整齐地打包在这个UIKit级别,所以它可能不适合你的项目,特别是如果你在C级别不舒服.但是,相关功能是CGContextDrawPDFPage;按照常规CoreGraphics方式进行的,还有其他方法可以从数据源创建PDF引用,然后从PDF获取页面引用.然后,您需要处理缩放并转换为您想要的视图,并警告您需要执行水平翻转,因为PDF(和OS X)使用左下角作为原点,而iOS使用左上角.示例代码,在我的头顶:
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;
Run Code Online (Sandbox Code Playgroud)
猜测一下,您可能想要使用CGPDFPageGetBoxRect(pageRef, kCGPDFCropBox)获取页面的裁剪框,然后找出如何缩放/移动它以适合您的图像大小.
对于你的目的来说可能更容易的-renderInContext:方法是CALayer(参见QA 1703) - 获取截图的旧方法是UIGetScreenImage,但这绝不是真正的官方API,并且似乎暂时只是因为RedLaser的意外批准而被允许.使用QA中的代码,您可以自己设置从任何其他视图获取UIImage,而不必在屏幕上显示该视图.哪个可能解决了屏幕捕获的一些问题?虽然这意味着您只能支持OS 4.x.
在任何一种情况下,PDF都不能快速绘制.您可能需要填充表格,然后在后台线程上绘制缩略图,在可用时将它们向上推.你无法在后台线程上安全地使用UIKit对象,但所有CoreGraphics东西都是安全的.
| 归档时间: |
|
| 查看次数: |
8125 次 |
| 最近记录: |