最近有很多关于绘制PDF的问题。
是的,你可以很容易地用一个UIWebView渲染PDF,但是这不能给出你期望一个好的PDF查看器的性能和功能。
您可以绘制PDF页面到一个CALayer或到一个UIImage。苹果甚至有示例代码来展示如何绘制一个大的PDF 在可缩放的UIScrollview中
但同样的问题不断出现。
UIImage方法:
UIImage中的PDF格式不像图层方法那样具有光学尺度。UIImages限制生成PDFcontext时受到影响/防止使用它来创建新的缩放级别的实时呈现。CATiledLayer方法:
CALayer有很大的开销(时间):可以看到各个块呈现(即使是tileSize的调整)。CALayers无法提前准备(呈现在屏幕外)。一般来说,PDF阅读器的内存也很重。甚至监控苹果的可缩放PDF示例的内存使用情况。
在我当前的项目中,我正在开发一个PDF查看器,并在一个单独的线程中呈现一个页面的UIImage (这里也有问题!)并在规模为x1的情况下呈现出来。一旦缩放值大于1时,CATiledLayer呈现就会启动。iBooks采用了类似的双取方法,就像滚动页面一样,在出现清晰版本之前,您可以看到页面的较低分辨率版本仅不到一秒钟。
我呈现2页每页的每一边在焦点,以便PDF图像是准备好掩盖图层启动前,drawing.Pages再次销毁时,他们是+2页离聚焦的页面。
有没有人有任何见解,无论多小或多明显,都可以改善绘图PDF的性能/内存处理?或者这里讨论的其他问题?
编辑:一些提示(Credit,VdesmedT,Matt,Johann):
CGPDFPageRef快。NSOperations或GCD & 区块提前准备页面。CGContextSetInterpolationQuality(ctx, kCGInterpolationHigh); CGContextSetRenderingIntent(ctx, kCGRenderingIntentDefault);之前调用CGContextDrawPDFPage,以减少绘图时内存的使用NSOperations中使用docRef是个坏主意(内存),将docRef封装成一个单独的实例。NSOperations,特别是如果他们将使用内存,请注意不要打开上下文!其他PDF功能:
- [Understanding the PDF Rect for link positioning](https://stackoverflow.com/questions/4255298/how-does-an-annot-cgpdfdictionary-rect-translate-to-objective-c-points/4255586#4255586)
- [Converting PDF annot datestrings](https://stackoverflow.com/questions/4303394/converting-adobe-pdf-date-string-to-nsdate)
- [Getting the target of the link](https://stackoverflow.com/questions/4643489/how-do-i-retrieve-a-page-number-or-page-reference-for-an-outline-destination-in-a/4683905#4683905) (Getting the page number from the `/Dest` array)
Documentation
示例项目
发布于 2010-10-08 11:48:36
我使用近似相同的方法构建了这样的应用程序,除了:
UIImage重叠,而是在缩放为1时在图层中绘制图像。当发出内存警告时,这些瓷砖将自动释放。每当用户开始缩放时,我就获取CGPDFPage并使用适当的CTM呈现它。- (void)drawLayer: (CALayer*)layer inContext: (CGContextRef) context中的代码类似于:
CGAffineTransform currentCTM = CGContextGetCTM(context);
if (currentCTM.a == 1.0 && baseImage) {
//Calculate ideal scale
CGFloat scaleForWidth = baseImage.size.width/self.bounds.size.width;
CGFloat scaleForHeight = baseImage.size.height/self.bounds.size.height;
CGFloat imageScaleFactor = MAX(scaleForWidth, scaleForHeight);
CGSize imageSize = CGSizeMake(baseImage.size.width/imageScaleFactor, baseImage.size.height/imageScaleFactor);
CGRect imageRect = CGRectMake((self.bounds.size.width-imageSize.width)/2, (self.bounds.size.height-imageSize.height)/2, imageSize.width, imageSize.height);
CGContextDrawImage(context, imageRect, [baseImage CGImage]);
} else {
@synchronized(issue) {
CGPDFPageRef pdfPage = CGPDFDocumentGetPage(issue.pdfDoc, pageIndex+1);
pdfToPageTransform = CGPDFPageGetDrawingTransform(pdfPage, kCGPDFMediaBox, layer.bounds, 0, true);
CGContextConcatCTM(context, pdfToPageTransform);
CGContextDrawPDFPage(context, pdfPage);
}
}问题是与CGPDFDocumentRef有关的对象。我同步访问pdfDoc属性的部分,因为我释放它并在接收memoryWarnings时重新创建它。CGPDFDocumentRef对象似乎做了一些我没有找到的内部缓存。
发布于 2011-03-21 21:25:49
对于一个简单有效的PDF查看器,当您只需要有限的功能时,现在可以(iOS 4.0+)使用QuickLook框架:
首先,您需要链接到QuickLook.framework和#import <QuickLook/QuickLook.h>;。
之后,在viewDidLoad或任何延迟初始化方法中:
QLPreviewController *previewController = [[QLPreviewController alloc] init];
previewController.dataSource = self;
previewController.delegate = self;
previewController.currentPreviewItemIndex = indexPath.row;
[self presentModalViewController:previewController animated:YES];
[previewController release];发布于 2018-01-29 15:14:31
由于iOS 11,您可以使用称为PDFKit的本机框架来显示和操作PDF。
导入PDFKit后,应该使用本地或远程URL初始化PDFView,并将其显示在视图中。
if let url = Bundle.main.url(forResource: "example", withExtension: "pdf") {
let pdfView = PDFView(frame: view.frame)
pdfView.document = PDFDocument(url: url)
view.addSubview(pdfView)
}https://stackoverflow.com/questions/3889634
复制相似问题