简体   繁体   English

调用viewdidload中存在的UIImagePickerControler无效

[英]Calling UIImagePickerControler present in viewdidload Doesn't Work

I want to launch camera when my view controller loads. 我想在加载视图控制器时启动相机。 I tried calling takePhoto method in viewdidload but nothing happens, what did I do wrong? 我尝试在viewdidload中调用takePhoto方法,但没有任何反应,我做错了什么? I am getting the following error msg: "Attempt to present on whose view is not in the window hierarchy!" 我收到以下错误消息:“试图显示不在窗口层次结构中的视图!” When I inspect element UIImagePickerController, it is nil... Interestingly, I can place a button that calls this method and when I tap the button it works fine, but not when I call it in view did load... 当我检查元素UIImagePickerController时,它为nil ...有趣的是,我可以放置一个调用此方法的按钮,当我点击该按钮时,它可以正常工作,但是当我在视图中调用它时,它就无法加载...

- (IBAction)takePhoto {
UIImagePickerController *uiipc=[[UIImagePickerController alloc]init];
uiipc.delegate=self;
uiipc.mediaTypes=@[(NSString *)kUTTypeImage];
       uiipc.sourceType=UIImagePickerControllerSourceTypeCamera|UIImagePickerControllerSourceTypePhotoLibrary;
uiipc.allowsEditing=YES;
[self presentViewController:uiipc animated:YES completion:NULL];
 }

Try this: Call the camera from the view did appear method: 尝试以下操作:从视图确实出现的方法调用摄像机:

  //  makeCameraOff= YES;  call this in didFinishPickingMediaWithInfo method

-(void)viewDidAppear:(BOOL)animated{
  if (makeCameraOff==NO){
  [self performSelector:@selector(takePic) withObject:nil afterDelay:0];
 }
 }

-(void)takePic{

UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate =self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;

[self presentViewController:picker animated:YES completion:nil];


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM