views:

943

answers:

2

I can't seem to launch the camera when loading my view. I end up making the user have to find and press a button on the screen just to load the camera (redundant). How can I do this? Code follows:

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {

    self.imgPicker = [[UIImagePickerController alloc] init];
    self.imgPicker.allowsImageEditing = NO;
    self.imgPicker.delegate = self;
    self.imgPicker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [super viewDidLoad];

    [self presentModalViewController:self.imgPicker animated:YES];

}

UPDATE:
placing the above code into -viewDidAppear:(BOOL)animated allowed the camera to be launched, but the app crashed immediately afterward with the last routine being [UIWindowController transitionViewDidComplete:fromView:toView]; (as cited by Debugger)

A: 

Seems that it does not do it when you put the call to present modal view in view did load . You can try having a 2 second timer after the call to [super viewDidload] that pushes the picker view in or something like that.

Daniel
A timer will not solve the problem; it could be more than two seconds between when the view is first created in the controller and when it is about to be displayed, in which case the call would still not work.
Kevlar
+1  A: 

You should do it in viewWillAppear:, or viewDidAppear: if the first doesn't work. trying to do it in viewDidLoad won't work because that is called after the view is first created, and the view isn't a subview of anything else at that point. As far as i understand it, in order to call presentModalViewController on self, the view must at some level be displayed in the UIWindow.

One other thing i just noticed; your code leaks memory depending on how you declare your imgPicker property. if it is declared with retain instead of assign, then unless you explicitly release it twice somewhere that picker will always exist in memory. You should autorelease the init'd object as you assign it to the property in that case.

Kevlar
thanks Kevlar! You saved me from a potential leak headache.
RexOnRoids
UPDATE: - (void)viewDidAppear:(BOOL)animated worked in this case. Thanks!
RexOnRoids
UPDATE-2: Camera is showing but my app exits with : "Error EXC_BAD_ACCESS"
RexOnRoids
When does it exit? when the camera appears or when you dismiss it, or sometime after that?
Kevlar
App crashes immediately after the camera APPEARS. The last routine performed is: [UIWindowController transitionViewDidComplete:fromView:toView]; (as cited by the Debugger)
RexOnRoids
are you releasing the image picker anywhere else before you show it? that's all i can think of; EXC_BAD_ACCESS errors come up whenever you try to reference something that has been released enough for it to be deallocated. I can't really be of much more help without seeing the code itself.
Kevlar
No, I usually don't release the camera until dealloc of the calling viewController, and just in case, I commented out all releases to check this error and it doesn't seem to be related.
RexOnRoids
That's odd; the only thing i can think of is that you're releasing/autoreleasing something one too many times; scour through your code to make sure that everything you allocate is also released/autoreleased exactly once in the same scope it was alloc'd/retained/etc.
Kevlar
Could not find a solution. I ended up just using a button to launch the camera. Don't wanna waste too much time on one elusive yet trivial problem. Thanks for the help anyway though.
RexOnRoids