1

我在设置为 cameraOverlayView 的 UIView 中有一个 UIButton。

我还缩放了 uiimagepicker cameraView 以覆盖整个屏幕(如下图所示)。

在此处输入图像描述

这是我的代码:

 // CameraViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    if(!self.imagePickerController)
        [self setupCamera];
}

-(void)setupCamera{
    self.imagePickerController = [[UIImagePickerController alloc] init];
    self.imagePickerController.modalPresentationStyle = UIModalPresentationCurrentContext;
    self.imagePickerController.delegate = self;
    self.imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
    self.imagePickerController.showsCameraControls = NO;

    self.imagePickerController.cameraOverlayView =  self.overlayView;

    [self scaleCameraPreview];

    // Present picker in the next loop (to avoid warning - "Presenting view controllers on detached view controllers is discouraged")
    [self performSelector:@selector(presentPicker) withObject:nil afterDelay:0.0];
}

-(void)presentPicker{
    [self presentViewController:self.imagePickerController animated:NO completion:nil];
}

-(void)scaleCameraPreview{
    CGSize screenSize = [[UIScreen mainScreen] bounds].size;

    int heightOffset = 0;

    if(SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        heightOffset = 120;
    }

    float cameraAspectRatio = 4.0 / 3.0;
    float imageWidth = floorf(screenSize.width * cameraAspectRatio);
    float scale = ceilf(((screenSize.height + heightOffset) / imageWidth) * 10.0) / 10.0;
    self.imagePickerController.cameraViewTransform = CGAffineTransformMakeScale(scale, scale);
}

-(IBAction)cameraButtonPressed:(id)sender{
    [self.imagePickerController takePicture];
}

这是我的视图控制器层次结构:

- UINavigationController 
   - SWRevealViewController (manages a drawer functionality between different controllers)
     - UINavigationController (current controller for the camera)
       - CameraViewController 
         - PickerViewController (Presented modally as you can see above) 
           - PhotoTakenViewController (Controller that will fire after UIImagePicker returns an image)

编辑:添加覆盖信息

在此处输入图像描述

我在网上搜索了类似的线程(例如UIImagePickerController Overlay Buttons Not Firing),但没有找到任何解决方案。

您可以在图片中看到的 UIButton 位于 UIView 内部,这是分配给 cameraOverlayView 的代码中的 overlayView。它还通过插座连接到cameraButtonPressed:动作,如果我不添加UIImagePickerController,它可以完美运行。

它不响应任何我不知道为什么的触摸。

有谁知道发生了什么?

4

1 回答 1

0

我已经解决了我的问题,但我相信粗略的方式:

[self presentViewController:self.imagePickerController animated:NO completion:^(void){

    // Re-add the button so it is on top of the overlay
    [self.imagePickerController.view addSubview:self.takePhotoButton];
}];

稍后可能会回到这个问题并发布更新,以防我找到更好的解决方案。

于 2014-12-16T07:15:30.060 回答