iOS - 在UIImagePickerController CustomOverlayView中按下按钮后拍照

时间:2014-07-23 21:21:32

标签: ios uiimagepickercontroller

我已经搜索了类似的问题以获得答案,但我尝试过的任何内容都没有效果。当我点击叠加视图中的任一按钮时,相机仅会聚焦 - 不会触发任何操作。这是我的代码:

更新:上一个问题得到了解答 - 代码很好,我只是没有在testIfButtonResponds方法中提醒自己。现在我想知道如何关闭相机并在UIImageView中显示捕获的图片。

此外,UIImageView * DisplayedPhoto是在IB中制作的,所以它有一个框架。

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

0 个答案:

没有答案