views:

132

answers:

1

I have an odd problem with UIImagePickerControllerSourceTypeCamera. My application gives the choice to select a pic from the gallery, or take a photo with the camera. If I choose the gallery, I pick a photo and return to my view, no issues.

However, when using UIImagePickerControllerSourceTypeCamera, it appears to do something odd with my view when I return to it.

For example, I have a bunch of code in the viewDidLoad method which moves some objects in the view if it needs to based on some factors - this code gets called when I exit the UIImagePickerControllerSourceTypeCamera, but doesnt get called when I exit the gallery.

Is this expected?

+2  A: 

I think your view is getting dumped by the didReceiveMemoryWarning thing which is being triggered by the resource-intensive camera stuff. You can force the simulator to generate a memory warning without the camera to test this theory.

Generally speaking, viewDidLoad needs to be able to deal with getting called multiple times. It's not an init method. It gets called again if self.view gets set to nil and the view later needs to be recreated. There may be a more appropriate place to put any code you have there that's causing problems, but the init methods are tricky because the designated initializer is bypassed by nib loading.

When loaded from a nib, the class's initWithCoder is called instead which bypasses the whole init process because dearchiving is assumed to be sucking in an already-initialized object. Therefore reinitializing the object might break stuff, like call loadView which essentially conflicts with what a nib contains as it's supposed to programmatically construct what the nib already has in it. You can still override initWithCoder as usual though as long as you pass through the args to super like you should, but then this will not get called if you initialize the object with the designated initializer. Of course if you need to worry about that you can put all the code you want executed in both into a method that gets called from both overridden methods.

Nimrod
Thanks, this helps me understand a little. The memory warning didn't affect my view or app. It's odd, its as if the view is there, but it feels the need to run the viewDidLoad, and viewWillAppear methods when returning to it
mootymoots
I've also just noticed that as my view is part of a navigation view, it messes the stack up. If I go back from the view that the camera messed up to the root view, my root view then has another back button to go back to nowhere...Seems completely mental. My stack is then completely messed up and crazy, nothing but an app restart will fix it.To help people understand, I have a root view controller which creates a newView controller and pulls it into view. Then use the camera.
mootymoots
A reboot of the phone fixed this... ?!?!
mootymoots