我正在尝试在我的应用程序启动时打开相机 UI。问题是,我已经使用开始按钮完成了相机应用程序,单击该开始按钮,相机被打开并用于捕捉照片和视频。
我正在尝试从 viewDidLoad 方法调用 startCameraControllerFromViewController 但我面临以下异常
警告:尝试呈现不在窗口层次结构中的视图!
这是我的代码
- (void)viewDidLoad
{
imagePicker = [[UIImagePickerController alloc]init];
[self startCameraControllerFromViewController: self
usingDelegate: self];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (BOOL) startCameraControllerFromViewController: (UIViewController*) controller
usingDelegate: (id <UIImagePickerControllerDelegate,
UINavigationControllerDelegate>) delegate {
if (([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera] == NO)
|| (delegate == nil)
|| (controller == nil))
return NO;
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
cameraUI.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypeCamera];
cameraUI.allowsEditing = NO;
cameraUI.delegate = delegate;
[controller presentViewController: cameraUI animated: YES completion:nil];
return YES;
}