iOS – 在UIImagePickerController CustomOverlayView中点击button后拍照

我已经search了类似的问题的答案,但我没有尝试过的工作。 当我点击覆盖视图中的任一button时,摄像机只会对焦 – 不会触发任何操作。 这是我的代码:

更新:前面的问题得到了回答 – 代码很好,我只是没有在testIfButtonResponds方法中提醒自己。 现在我想知道如何closures相机并在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]; }