PrEV
Thoughts from a NeXTStep Guy on Cocoa Development

CATiledLayer Example

Jun 13, 2008 by Bill Dudney

One of the examples I put off until I had to do it was CATiledLayer. Basically the docs were not very complete so I was hoping that by the time I really needed them I'd have better docs. Well the docs never materialized but I had 2 or 3 errata about the lack of an example so I figured I'd get off my back side and get something done. I did an example that will make it into the book. It is on drawing png's with multiple versions of the image that are drawn depending on the scale factor, its cool you will like it :).

In the mean time there were still a few things that seemed like I did not grok. So, I went to the CA lab time while out here at WWDC (besides networking that is the other 'coolest thing' about being here). After spending some quality time with the folks in the lab I think I finally have the tiled layer nailed.

I've uploaded the example here.

The example shows a delegate doing all the drawing (which is drop dead simple) and the view setting up the layers. The thing I had wrong in my head was related to levelsOfDetail and levelsOfDetailBias. Levels of detail determines the # of levels of detail (seems obvious doesn't it...) and the bias determines how many are 'reserved' for zooming in. So if you set the levels of detail (LOD) to 4 and the bias to 1, you'd get 2x zooming in (2^1) 'native zoom' (2^0), 2x zoomed out (2^-1) and 4x zoomed out (2^-2). If you set the LOD to 8 and the bias to 5 you'd get 32x zoomed in (2^5) to 4x zoomed out (2^-2). Hope that makes sense, if not feel free to ping me.

Happy animating...



Comments:

TiledDelegate.m is missing an #import "TiledDelegate.h"

Thanks for the levelsOfDetailBias explanation. I hadn't figured that out from Apple's docs.

Posted by Tim Buchheim on June 13, 2008 at 11:59 AM MDT #

Hi Tim,

Thanks!

I've uploaded a new version with the stuff fixed.

Posted by Bill Dudney on June 13, 2008 at 12:02 PM MDT #

can you explain the purpose of loading a pdf with a tiled layer? i figure the use of a tiled layer to to load tiles like how google maps works? but you are working off one pdf so why would you do this instead of just loading the pdf and scaling it?.

Posted by aaron alexander on June 15, 2008 at 01:54 AM MDT #

Hi Aaron,

Couple of reasons;

- While a use case like google maps is certainly important for the CATiledLayer the big deal is that you can use the tiled layer to show content that won't fit into a texture. This PDF scaled up to 32x still looks great and fits that bill nicely. I don't have much jpg or png content that i could say the same about.

- The example does more or less what another example that I saw @ wwdc did (but can't talk about because of the nda). Since the presenter could not share the code with me I rebuilt the example from scratch to make sure I understood what was going on with the tiled layer.

- The example is intended to show how to interact with the tiled layer more than how to draw tiled content.

You are not however the first person to ask. If I have time I'll put a really big jpg into the example, tiled it and show how to mess with the CTM and bounding box of the context to figure out which tiles to draw.

Thanks for the comment!

Posted by Bill Dudney on June 15, 2008 at 02:20 AM MDT #

Pre-ordered your book so anxiously awaiting it. I could sure use some additional help with the levelsOfDetail and bias. Using your example above of 8 and 5 above, let me add some more constraints.

I have some bit map image of size 1024x1024 with a tile size of 32x32. If the bounds of the enclosing view are at 100%, then when my delegate gets called, I call CGContextGetClipBoundingBox() and by dividing the origin by 32 I can get the column and row of my tile. In this example I would have 32x32=1024 tiles

So, lets say I want to zoom in by 2x. Is my tile size now 64x64? Likewise if I zoom out its 16x16? If this is true, well, then the number of tiles will stay the same zooming in or zooming out as long as the factor is within the levels of details settings.

So, what happens when I hit a boundary? That is, with 8/5, I can zoom up to 32x. If I set my view's boundary to force a 64x size increase, what happens? Does the tile size get pegged? This would imply that my delegate will get asked to draw 2n tiles (32x32x2=2048). Or do I get an exception/crash?

Thanks for any clarification.

Posted by David Hoerl on August 01, 2008 at 06:01 AM MDT #

Hi.

I was wondering if you could briefly touch base on if it is possible to get the same functionality out of the iPhone as you've done here? I know the code and functions exist for the iPhone that you use here but, I'm running into performance issues and was wondering if its simply lacking on the iPhone or my programming skills... I'm hoping for the latter. I'm basically doing exactly what you've done here but it takes a month and a day to load the tiles with a relatively small pdf (<600kb). Any direction would be a great help!!! Thanks.

Posted by Preston on November 22, 2008 at 06:32 AM MST #

Any chance you can convert this example to use an PNG graphic? I have been trying to, but I have not managed to get it to work properly.

Posted by michael on December 26, 2008 at 04:24 PM MST #

Hi Michael,

The code for the CA book (http://www.pragprog.com/titles/bdcora) has an example with a really big png file.

Posted by Bill Dudney on December 26, 2008 at 04:25 PM MST #

Very interesting post.
Thanks.

But I still have questions.

I am now developing PDFViewer on iOS.
Till now, most of the obstacles were solved.
The only one I did not solve yet is about the levelsOfDetails.

In iBooks app on iOS, if I turn the page by swiping, the new page is displayed at once.
No tiling happens.
If I zoom-in or zoom-out that page, then tiling starts.

Is it a secret of levelsOfDetail and levelsOfDetailBais ?
To what value should I set on levelsOfDetail and levelsOfDetailBais
if I want to display PDF pages without tiling ?

I tried this way.
First set levelsOfDetail = 1 and levelsOfDetailBais = 0.
In that case, no tiling happens.
But zooming causes problems by levelsOfDetail = 1 and levelsOfDetailBais = 0.
So if I change those values after initial rendering,
the page is re-rendered.

How can I achieve iBooks features ?

Thanks in advance.

Posted by Sunho Park on September 02, 2011 at 01:09 PM MDT #

Post a Comment:
  • HTML Syntax: Allowed