tags:

views:

164

answers:

2

Due to the lazy loading nature of view controllers in iPhone, my app is a bit slow in response the first time when you go to the various screens, which has a rich graphics elements (custom graphics assets) in it. Will preloading the app images ahead of time, that is, by calling [UIImage imageNamed: ...] ahead of time so the next time it's called a cache version is used instead.

Will this work? Any other tips that can speed up the loading of view controllers where rich UI is involved (i.e. lots of custom graphic assets used)?

A: 

Sending +[UIImage imageNamed:] does cache the image you're trying to load, so sending this message ahead of time should speed up subsequent image grabs (assuming they're in the cache) according to the docs. imageNamed: documentation is here.

fbrereto
A: 

You can also load the view controllers ahead of time and then call [viewController view] to force the view to load.

I don't think there's a guarantee that image or nib loading is safe to call from another thread. Image-loading might be safe if they've made the image cache thread-safe (NSMutableDictionary is thread-safe IIRC), nib loading is less so because init/viewDidLoad/etc might expect to run in the main thread.

If you do pre-load things in the main thread, then it'll block the UI. You can mitigate this e.g. by using performSelector:withObject:afterDelay: and a small delay (0.01? 0.1?); note that there's no guarantee. On OS 4, you can load each image in an operation on [NSOperationQueue mainQueue]; setting the operation priority to something small (0 or 0.1?) should ensure that the UI gets priority.

Also note that any pre-loading is a bit pointless if you use so much memory that you get a memory warning.

tc.