Using cameraOverlayView with UIImagePickerController

前端 未结 3 1622
小鲜肉
小鲜肉 2020-12-31 13:58

I know this has been asked a number of time but I can\'t find an answer.

I have an app that uses the UIImagePickerController to take a picture. The prob

3条回答
  •  轮回少年
    2020-12-31 14:39

    Here is my code. If you want the camera to appear as soon as the view controller opens, make sure you initialize the UIImagePickerController in viewDidAppear like I did (viewDidLoad does not work).

    @interface CameraViewController ()
    @property UIImagePickerController *PickerController;
    @property CGFloat HeightOfButtons;
    @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 two red buttons on the bottom of the view (on top of the live camera feed)
        // Then adds the buttons to the main overlay view
        // You can, of course, customize these buttons however you want
        // ------------------------------------------------------------------------
        for(int i = 0; i < 2; i++) {
            self.HeightOfButtons = 100;
            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];
    }
    

提交回复
热议问题