8

Here is the code:

https://www.dropbox.com/s/o42wy36x4qhrbpt/PDFScroller.zip

I took the WWDC 2010 PhotoScroller sample code that implements nested UIScrollViews for zooming, inside a UIScrollView for paging, and swapped out what I thought would be minimal amount of code required for displaying a multi-page PDF instead of images.

It works. But it's slow on my iPhone4, about three seconds to paint the first page, and even slower on my iPod Touch. I can watch it painting the individual tiles. This same PDF already opens up more quickly, with no visible tile drawing, in an alternate CATiledLayer implementation I have which simply uses a single CATiledLayer/UIScrollView and touch events to change pages. I'd like to use this PhotoScroller technique, it's very nice.

I watched it with CPU Sampler in Instruments, and it doesn't seem to be the PDF rendering code, it looks like the time is taken up in threading and messaging. I'd appreciate it if someone could help point out what this sample is doing to incur the overhead.

Thanks,

Jim


Update 1: I had originally used the TilingView class technique from the sample code of defining

+ (Class) layerClass {
  return [CATiledLayer class];
}

And then drawing in - (void)drawRect:(CGRect)rect but switched to the explicit CATiledLayer subclass as a first attempt at seeing whether it would make a difference, but it did not, and so I left the code as-is for posting here. There is also a missing [tiledLayer release]; leak in TilingView.

4

3 回答 3

5

看到这个适用于 iPhone / iPad / iOs 的 Thread Fast and Lean PDF Viewer - 提示和提示?PDF 渲染技巧。

于 2010-10-27T20:19:23.337 回答
2

由于您的代码包含几个错误并且我无法编译代码,但我查看了存档中包含的 PDF 文件,我知道您的 TilingView 速度慢的原因。

CGContext通常,当您使用 : 方法在 a 中绘制 pdf 页面时CGContextDrawPDFPage,所有文本和矢量图形都会被渲染,而 PDF 中的普通图形等其他内容只会被绘制和缓存。因此,PDF 文件有多大并不重要,但如果您的 PDF 中有矢量图形,这确实很重要。您的 PDF 中似乎有一些矢量图形和一些数学方程式,这就是它速度慢的原因。我建议您尝试使用另一个不包含矢量图形的 PDF 文件,看看它是否更快。

干杯

于 2010-08-17T08:21:12.467 回答
1

不得以任何权威进行推测:PDF大多数文档的基于矢量的格式(不包括那些仅用作嵌入式 TIFF 图像容器的格式)。因此,当您像 PhotoScroller 一样平铺 PDF 时,您实际上是要求手机为每个单独的平铺缩放和栅格化整个 PDF(至少是给定页面)。因此,与其为单个 CATiledLayer 绘制一次,不如为每个分辨率绘制多次。由于 PDF 本身是一种可缩放格式,因此您应该能够为每个页面/比例简单地呈现整个视图一次。

更新:我自己刚刚经历了这个,PhotoScroller 示例有一些逻辑问题,使其非常慢。基本上,它以 1/zoomScale 渲染每个图块,然后比例返回到 zoomScale。因此,如果缩放为 0.5,它会以2倍渲染,然后将其缩小到 0.5 倍。很慢很低效。

于 2010-09-17T16:15:03.270 回答