iOS - Taking A Picture After Button Tap in UIImagePickerController CustomOverlayView

本小妞迷上赌 提交于 2019-12-11 18:49:22

问题


I have searched similar questions for answers, but nothing I have tried has worked. When I tap on either button in my overlay view, the camera only focuses - no action is triggered. Here is my code:

Update: The previous question was answered - the code was fine, I just wasn't alerting myself in the testIfButtonResponds method. Now I am wondering how to dismiss the camera and show the captured picture in a UIImageView.

Also, the UIImageView *DisplayedPhoto was made in IB, so it has a frame.

@interface CameraViewController ()
@property UIImagePickerController *PickerController;
@property (weak, nonatomic) IBOutlet UIImageView *DisplayedPhoto;
@end


- (UIView *)createCustomOverlayView
{

    // Main overlay view created
    UIView *main_overlay_view = [[UIView alloc] initWithFrame:self.view.bounds];

    // Clear view (live camera feed) created and added to main overlay view
    // ------------------------------------------------------------------------
    UIView *clear_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.HeightOfButtons)];
    clear_view.opaque = NO;
    clear_view.backgroundColor = [UIColor clearColor];
    [main_overlay_view addSubview:clear_view];
    // ------------------------------------------------------------------------


    // Creates the buttons on the bottom of the view (on top of the live camera feed)
    // Then adds the buttons to the main overlay view
    // ------------------------------------------------------------------------
    for(int i = 0; i < 2; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        // when a button is touched, UIImagePickerController snaps a picture
        [button addTarget:self action:@selector(testIfButtonResponds) forControlEvents:UIControlEventTouchUpInside];
        button.frame = CGRectMake( i * self.view.frame.size.width / 2, self.view.frame.size.height - self.HeightOfButtons, self.view.frame.size.width / 2, self.HeightOfButtons);
        [button setBackgroundColor:[UIColor redColor]];
        [main_overlay_view addSubview:button];
    }
    // ------------------------------------------------------------------------

    return main_overlay_view;
}

- (void)makeCustomCameraAppear
{
    self.PickerController = [[UIImagePickerController alloc] init];
    self.PickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    self.PickerController.showsCameraControls = NO;
    self.PickerController.delegate = self;

    UIView *overlay_view = [self createCustomOverlayView];
    [self.PickerController setCameraOverlayView:overlay_view];

    [self presentViewController:self.PickerController animated:YES completion:NULL];
}

- (void)viewDidAppear:(BOOL)animated
{
    [self makeCustomCameraAppear];
}

- (void) testIfButtonResponds
{
    [self.PickerController takePicture];
    [self.PickerController dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *chosen_image = info[UIImagePickerControllerEditedImage];
    self.DisplayedPhoto.image = chosen_image;
    [self.PickerController dismissViewControllerAnimated:YES completion:NULL];
}

来源:https://stackoverflow.com/questions/24921240/ios-taking-a-picture-after-button-tap-in-uiimagepickercontroller-customoverlay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!