简体   繁体   English

单击按钮关闭UIImagePickerView

[英]Dismiss the UIImagePickerView on button click

I am using image picker to take the pictures and save it into database. 我正在使用图像选择器拍摄照片并将其保存到数据库中。 Once I save it in database, i put an alert view saying image is saved and when i click ok, i need to dismiss the image picker view and i need to present the another view . 将其保存在数据库中后,我将放置一个警报视图,说图像已保存,当我单击“确定”时,我需要关闭图像选择器视图,并且需要显示另一个视图。

Here is what i have 这是我所拥有的

-(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];

    }
}


}

I can dismiss the picker view, if i hadnt place the [self takepic] in the view did appear. 如果我没有将[self takepic]放置在视图中,则可以取消选择器视图。

I also tried 我也试过

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

but i couldnt dismiss the picker view. 但是我无法解散选择器视图。 I am getting the warning: "Warning: Attempt to present on whose view is not in the window hierarchy!" 我收到警告:“警告:尝试呈现其视图不在窗口层次结构中的视图!”

Can anyone tell me,how to dismiss the picker view 谁能告诉我如何消除选择器视图

Try this. 尝试这个。 I guess it will help you In you code, you are taking a new referene of UIImagePicker by UIImagePickerController *picker; 我想它将对您有帮助。在您的代码中,您正在通过UIImagePickerController *picker; picker获取UIImagePicker的新参照UIImagePickerController *picker; you should dismiss the same image picker 您应该关闭相同的图像选择器

-(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];
}

You need to make a property to switch the camera on and off 您需要设置属性以打开和关闭相机

@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;
 }

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

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