Ios 无法弹出UIImagePickerController

Ios 无法弹出UIImagePickerController,ios,objective-c,uiimagepickercontroller,Ios,Objective C,Uiimagepickercontroller,我有一个带有自定义覆盖的UIImagePickerController。我正在尝试实现一个取消按钮,它将弹出UIImagePickerController。我的设置代码如下: - (void)tapPhoto { //setup the picker self.picker = [[UIImagePickerController alloc] init]; self.picker.delegate = self; self.picker.sourceType =

我有一个带有自定义覆盖的UIImagePickerController。我正在尝试实现一个取消按钮,它将弹出UIImagePickerController。我的设置代码如下:

- (void)tapPhoto {

    //setup the picker
    self.picker = [[UIImagePickerController alloc] init];
    self.picker.delegate = self;
    self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    self.picker.cameraDevice=UIImagePickerControllerCameraDeviceFront;
    self.picker.showsCameraControls=NO;

    self.cameraOverlay = [[CameraOverlay alloc] init];
    UIImage *camoverlayImage = self.cameraOverlay.cameraOverlayImage;
    UIImageView *camoverlayImageView = [[UIImageView alloc] initWithImage:camoverlayImage];
    camoverlayImageView.frame=CGRectMake(0, 0, self.cameraOverlay.previewSize.width , self.cameraOverlay.previewSize.height);

    //init the control view
    UIView *cameraControls= [[UIView alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height)];
    [cameraControls addSubview:camoverlayImageView];

    //Shutter Button
    UIButton *button = [[UIButton alloc] init];//70*70
    [button setBackgroundImage:[UIImage imageNamed:@"cameraoff"] forState:UIControlStateNormal];
    [button setBackgroundImage:[UIImage imageNamed:@"cameraon"] forState:UIControlStateHighlighted];
    button.frame = CGRectMake(([[UIScreen mainScreen] bounds].size.width/2)-70/2, [[UIScreen mainScreen] bounds].size.height-70-10, 70 , 70);
    [button addTarget:self action:@selector(takePhoto) forControlEvents:UIControlEventTouchUpInside];
    [cameraControls addSubview:button];


    UIButton *button2 = [[UIButton alloc] init];
    [button2 setTitle:@"Cancel" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(cancelPhoto) forControlEvents:UIControlEventTouchUpInside];
    button2.frame = CGRectMake(5, [[UIScreen mainScreen]bounds].size.height-30, 0, 0);
    [button2 sizeToFit];
    [cameraControls addSubview:button2];

    [self.picker setCameraOverlayView:cameraControls];
    [self presentViewController:self.picker animated:YES completion:NULL];

}

-(void)cancelPhoto{

    NSLog(@"photo canceled");
    [self.navigationController pushViewController:self.picker animated:YES];



}

-(void)takePhoto{
    //this will auto trigger the control
    [self.picker takePicture];
}

我试图从导航控制器中弹出它,但没有任何运气。我也尝试过[self.picker popViewControllerAnimated:是];。有谁能给我指点一下为什么这样不行吗?谢谢

您正确地展示了您的选取器:

[self presentViewController:self.picker animated:YES completion:NULL];
“现在”的反面不是“推”或“弹出”,而是“驳回”:


您正确地呈现了选择器:

[self presentViewController:self.picker animated:YES completion:NULL];
“现在”的反面不是“推”或“弹出”,而是“驳回”:

首先,这条线错了

在当前视图控制器中已显示选择器时,为什么要推送选择器

正确的方法是:-

首先,这条线错了

在当前视图控制器中已显示选择器时,为什么要推送选择器

正确的方法是:-

像这样试试-

-(void)tapPhoto {
   //set up the Picker
    self.picker = [[UIImagePickerController alloc] init];
    self.picker.delegate = self;
    self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    self.picker.cameraDevice=UIImagePickerControllerCameraDeviceFront;

   //add a UIView as CameraOverLay   
      UIView *overlayView = [[UIView alloc] initWithFrame:picker.view.frame];
     [self.picker setCameraOverlayView:overlayView];

    //Present the Picker 
     [self presentViewController:picker animated:YES completion:nil];
}
这样,默认情况下应该有一个取消按钮。然后,只需实现委托方法,并在单击“取消”按钮时关闭控制器

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

      //get the captured image if any
    if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
            == kCFCompareEqualTo){
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

       //save the image in photo library
       UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }

     //pop out or remove the picker
     [picker dismissViewControllerAnimated:YES completion:nil];
    }
像这样试试-

-(void)tapPhoto {
   //set up the Picker
    self.picker = [[UIImagePickerController alloc] init];
    self.picker.delegate = self;
    self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    self.picker.cameraDevice=UIImagePickerControllerCameraDeviceFront;

   //add a UIView as CameraOverLay   
      UIView *overlayView = [[UIView alloc] initWithFrame:picker.view.frame];
     [self.picker setCameraOverlayView:overlayView];

    //Present the Picker 
     [self presentViewController:picker animated:YES completion:nil];
}
这样,默认情况下应该有一个取消按钮。然后,只需实现委托方法,并在单击“取消”按钮时关闭控制器

 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{

      //get the captured image if any
    if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
            == kCFCompareEqualTo){
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

       //save the image in photo library
       UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }

     //pop out or remove the picker
     [picker dismissViewControllerAnimated:YES completion:nil];
    }
不要尝试使用popViewControllerAnimated。相反,请尝试:

[self.picker dismissViewControllerAnimated:YES completion:nil];
不要尝试使用popViewControllerAnimated。相反,请尝试:

[self.picker dismissViewControllerAnimated:YES completion:nil];