Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
350 views
in Technique[技术] by (71.8m points)

uikit - How to launch iPhone Camera on viewDidLoad?

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)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...