tags:

views:

309

answers:

2

In my NiB, I have several View controllers. When the app launches, all their corresponding ViewDidLoad() calls are called.

in ViewControllerA, I have ViewA that contains a UiScrollView with 100 pictures. In initalize these pictures in ViewdidLoad. So on app launch, all pictures are initalized.

In my Main Menu View Controller, I have a button that when pressed calls presentModalViewController:ViewControllerA

That call is taking 20 seconds.

I thought the pre loading in ViewDidload would optimize this. However, once the call returns and recalled (bringing up this VC the second time, its really quick and instant).

So what is causing this? Why is it taking so long the first time calling presentModalViewController. Internally, is it trying to arrange all the images? Any way of optimizing this? If not, whats the best way to show UiActivityIndicator

Thank you in Advance

A: 

First, put some NSLog() statements in your -viewDidLoad methods. They probably aren't being called when you think they are. UIViewControllers are lazy. They don't actually load their NIB until someone references their -view.

If by "all their corresponding ViewDidLoad() calls are called," you mean you're actually calling [viewController viewDidLoad] by hand, that won't work and don't do that. That's a delegate method that should only be called by the system.

You could, but shouldn't, get around all this by calling [viewController view]. This will force a load, but this is almost always the wrong answer.

How big are the pictures? Do you really have the memory available to hold all 100 all the time? Could you load these pictures as you need them? What shape are the pictures laid out? Could a UITableView do a lot of the work for you? If their in a grid, you could show 4-5 pictures per UITableViewCell, and then you'd get all of UITableView's optimizations.

Rob Napier
I have all my VCs in one XIB and i checked with NSLOG messages that viewDidLoad are getting called upon app launch :(My App is in landscape and I want a ScrollView that scrolls horzontial with pictures on it. What recommendation would you give for that kind of implementation
+1  A: 

This turned out to be too long for a comment.

I would first play around with UITableView to see if it could apply.

If it's completely fullscreen, then you have the UITableView be in portrait, but rotate your pictures in their cells. This will be identical to landscape.

If the scrollview doesn't fill the screen, you can still try UITableView with a view transform to rotate it.

Then your next approach should be to put in placeholders in the scrollview that lazily load the images. If all the images are the same size, then this is very straightforward, and you can optimize memory by only holding a limited number of these cells.

Another crazy idea: make it a UIWebView. That will do all the lazy-load for you.

Rob Napier
Thanks Rob for your response, I really appreciate it. I am trying to optimize the way I load my images in my scroll view (loading one by one). Instead of having my initalization in LoadView. I am having them in ViewDidLoad:Animated. However, I cannot get them to show up one by one (so the user sees progress). They just all appear. Could you elobrate on your placeholder approach ? I would like to implement this
You need a new UIImageView subclass. It should provide a placeholder image of the correct size while it fetches the real image. If you're fetching from disk, then you need to put the real image fetch on a background thread. If you're fetching from the network, you can use NSURLConnection to load it on the main thread without blocking. When the UIImage is done loading, you will call setImage: to swap out the placeholder for the real one. You may have to call setNeedsDisplay: at that point as well to get it to redraw.
Rob Napier