R – Parent View issues with UIImagePickerControllerSourceTypeCamera

cameraiphoneuiimagepickercontroller

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?

Best Answer

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.

Related Topic