【问题标题】:Dismiss the UIImagePickerView on button click关闭按钮单击时的 UIImagePickerView
【发布时间】:2014-12-19 17:43:46
【问题描述】:

我正在使用图像选择器拍摄照片并将其保存到数据库中。一旦我将它保存在数据库中,我会放置一个警告视图,说图像已保存,当我单击确定时,我需要关闭图像选择器视图并且我需要呈现另一个视图。

这就是我所拥有的

-(void)viewDidAppear:(BOOL)animated{

[super viewDidAppear:animated];

[self takePic];


}

-(void)takePic{

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

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

}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{

if (alertView.tag==1) {

    if (buttonIndex==0) {
        UIImagePickerController *picker;
         [picker dismissViewControllerAnimated:YES completion:NULL]; // here i am not able to dismiss the image picker view
        Tab *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"Tabbar"];
        [self presentViewController:vc animated:YES completion:nil];

    }
}


}

如果我没有在视图中放置 [self takepic],我可以关闭选择器视图。

我也试过了

        [picker dismissViewControllerAnimated:YES completion:^{
            [self presentViewController:vc animated:YES completion:NULL];
        }];

但我无法关闭选择器视图。我收到警告:“警告:尝试显示不在窗口层次结构中的视图!”

谁能告诉我,如何关闭选择器视图

【问题讨论】:

    标签: ios uiimagepickercontroller


    【解决方案1】:

    试试这个。我想它会帮助你 在您的代码中,您正在通过UIImagePickerController *picker; 获取 UIImagePicker 的新引用 您应该关闭相同的图像选择器

    -(void)takePicture{
    
        picker = [[UIImagePickerController alloc] init];
        picker.delegate =self;
        picker.allowsEditing = YES;
        picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
        [self presentViewController:picker animated:YES completion:NULL];
    
    }
    
    - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    
        if (alertView.tag==1) {
    
            if (buttonIndex==0) {
                [picker dismissViewControllerAnimated:YES completion:NULL]; // here i am not able to dismiss the image picker view
    
            }
        }
    }
    
    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
    
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Hello" delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        alert.tag = 1;
        [alert show];
    }
    

    【讨论】:

      【解决方案2】:

      您需要创建一个属性来打开和关闭相机

      @property (assign) BOOL *makeCameraOff;
      
      
      -(void)viewDidAppear:(BOOL)animated{
      
      
      if (makeCameraOff==NO) {
      
          [self performSelector:@selector(takePic) withObject:nil afterDelay:0.3];
      }
      }
      
      -(void)takePic{
      
      UIImagePickerController *picker = [[UIImagePickerController alloc] init];
      picker.delegate =self;
      picker.allowsEditing = YES;
      picker.sourceType = UIImagePickerControllerSourceTypeCamera;
      
      [self presentViewController:picker animated:YES completion:NULL];
      
      }
      
      
      - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
      
        //here change the BOOL value to YES,so that you can dismiss the camera view
      makeCameraOff=YES;
      
      [picker dismissViewControllerAnimated:YES completion:NULL];
      
      
       }
      
      
      - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
      
      
      // your code to present the view controller you need;
       }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-09-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-26
        相关资源
        最近更新 更多