How to launch iPhone Camera on viewDidLoad?

I cannot start the camera while loading my view. Ultimately, the user must find and press a button on the screen to load the camera (redundancy). 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:
by placing the above code in the -viewDidAppear: (BOOL) animation, you could start the camera, but the application crashed immediately, and the last procedure was [UIWindowController transitionViewDidComplete: fromView: toView]; (as quoted by the debugger)

+4
source share
2 answers

You must do this in viewWillAppear :, or viewDidAppear: if the first does not work. trying to do this in viewDidLoad will not work, because it is called after the view is first created, and the view is not an approach to anything else at this point. As far as I understand, to call currentModalViewController on itself, the view should be displayed at some level in UIWindow.

One more thing I just noticed; your code skips memory depending on how you declare your imgPicker property. if it is declared with preservation instead of assignment, then unless you explicitly release it twice somewhere, this collector will always exist in memory. You must auto-detect the init'd object when you assign it in this case.

+4
source

It does not seem to do this when you place a call to present a modal view in the field of view loaded. You can try having a 2 second timer after calling [super viewDidload], which pushes the view of the selection or something like that.

0
source

All Articles