I’m trying to understand how CATiledLayer works when rendering a PDF page using CGContextDrawPDFPage().
My understanding was that drawLayer:inContext: is called once per tile and level of detail. Is that correct?
In the delegate of my tiled layer I call CGContextDrawPDFPage().
However I noticed (by putting logs into the drawLayer:inContext:) that drawLayer:inContext: gets called more often if I make the tile size of my tiled layer smaller.
This makes me wonder:
- Is my PDF page drawn/rendered x times?
- Or does CGContextDrawPDFPage() magically know what part of the page to draw?
- Isn’t CATiledLayer a waste of resources when rendering a PDF page? What is its advantage?
Yes, of course. Assuming your layer’s size is the same, then if you tell it to use smaller tiles, it will need more tiles to cover the area.
Since you call CGContextDrawPDFPage() once per tile, then yes.
However, that may not be a problem, because:
It might, but it doesn’t require any magic.
Before CATiledLayer calls
-drawLayer:inContext:, it sets the clip of the CGContext to include only the area of that tile. The PDF drawing code can get the clip bounds (viaCGContextGetClipBoundingBox) and then choose to render only the content inside that rectangle.That said, there are two caveats:
CGContextDrawPDFPage()is a black box, so it might not actually do that optimization. It seems obvious, but you’ll have to check the performance numbers to see if it really is happening or not.CGContextDrawPDFPage— it really depends on how smart CG is, and whether it caches things internally or not.The advantage of CATiledLayer is mostly in memory savings: the bitmaps that rendered content are stored in are very large, so it pays to render and keep only the content that is currently visible on screen. It also provides a mechanism for caching the areas that were recently visible or might become visible again soon, depending on how the user zooms and scrolls.
The tradeoff is: you have to separate your drawing into separate calls for each tile, and you may have to render into tiles more often than you would have otherwise.
Depending on how large your view is, how far you zoom in/out, how expensive your PDF is to draw, and what else is happening in your application, it might make sense to use CATiledLayer or it might not. It’s up to you to look at the performance numbers and decide.