TiledLayer on the iPhone
Dec 08, 2008 by Bill Dudney
I finally got the tiled layer example ported to the phone. The SFMuni map was killing performance and takes forever to draw so I did a simple keynote slide with some text and a few shapes so it would not be too simple. This slide PDF draws very fast on my 3G iPhone. You can grab the code here. If you want to go back to the SFMuni map, just change the code in the sfMuni method of TiledDelegate class.
As the comments in the code say this is not an attempt to teach you how to handle events. I just hacked some stuff together enough to get zooming and panning working well enough to experiment with the pdf drawing so use it as that.
I hope it helps!









Bill,
Sorry, but what is this TiledLayer example you are referring to? Looks like I'm missing some context here as to what this is related to.
--Joao
Posted by Joao Prado Maia on December 08, 2008 at 04:37 PM MST #
Sorry Joao,
I've updated the entry now to have a link to the old entry.
HTH!
Posted by bill Dudney on December 08, 2008 at 04:42 PM MST #
Hi Bill,
While the layer works fine when using [UIImage imageNamed:...].CGImage in the TiledDelegate, if that is replaced with a CGImageRef made by calling CGImageCreateUsingJPEGDataProvider, the tiles load painfully slow.
The obvious choice is just to use .CGImage, but in my case I need to be able to release the CGImage from memory when I'm not using it but then bring it back again when I need it. CGImageRelease will cause it to release permanently. Any ideas?
Thanks,
Dan
Posted by Dan on February 03, 2009 at 04:02 AM MST #
Hi Dan,
Loading a JPEG requires a conversion to PNG behind the scenes so that could be a big part of the slow down. Also make sure you are loading only what you need and not a whole huge image.
Good Luck!
Posted by Bill Dudney on February 03, 2009 at 04:07 AM MST #
How do you load a part of a huge image? I haven't seen a method in UIImage that would do anything like that. What method would you be referring to that can load only portions of an image?
Posted by Issac on April 10, 2009 at 01:44 PM MDT #
Hi Issac,
You have to use Core Graphics routines, google 'loading large images with core graphics' and you should see some stuff that will help.
Good luck!
Posted by Bill Dudney on April 11, 2009 at 02:35 AM MDT #
Bill,
so you have a conversion to the iPhone of the CATiled example in the book drawn with slices?
Thanks
Michele
Posted by Michele on April 22, 2009 at 02:42 AM MDT #
Bill,
After going through your code I started wondering why you made the tiled layer a sublayer of the UIView's layer versus making the UIView backing layer tiled (by using +layerClass)? In my simple explorations, it looks like you can proceed down the +layerClass path and not incur the overhead of two fully overlapping layers.
Do you have any thoughts or comments on why I should avoid using +layerClass?
Andrew
Posted by Andrew on June 29, 2009 at 05:56 AM MDT #
Hi Andrew,
All UIViews are layer backed on the phone, wether you implement +layerClass or not. Also the overhead for adding an additional layer is almost negligible. That said, you should more or less leave the layer that is owned by the view alone. There is a special relationship between a view and its layer and if you start manipulating the layer you can introduces some strange behavior.
Much better to add a sublayer.
Posted by Bill Dudney on June 29, 2009 at 06:01 AM MDT #
Bill,
Thank you for your reply and I have another question.
You say the resource cost of a second sublayer is negligible. Yet, layer image allocations do not appear to show up in Instruments. Is there a way to actually find out how much a layer is costing me? I'm not doubting your statement. I just want to know how to figure these things out myself?
Andrew
Posted by Andrew on June 30, 2009 at 12:23 PM MDT #
You could try using the object allocation instrument and just make a tight loop to create 1000 layers, that should give you a nice bump in memory usage that would help you see the memory each layer takes.
Have you used/looked at the core animation instrument it has lots of nobs and dials, perhaps one of them will let you see the resources an individual layer is using.
Good luck!
Posted by Bill Dudney on June 30, 2009 at 12:26 PM MDT #