configuring frame size of UIImagePickerController

后端 未结 3 528
时光取名叫无心
时光取名叫无心 2020-12-29 14:37

Is it possible to change the displayable frame size of UIImagePickerController? I want to display camera view but not on the entire screen, but say in a 100x100 bounding box

3条回答
  •  -上瘾入骨i
    2020-12-29 14:42

    If you want to display an overlay on top of the UIImagePickerController, such as in my case, while at the same time fitting the controller's "live camera feed" (the view) in between some UI components on your overlay (such as a top bar and a bottom bar), use the following code. This is based on SomaMan's answer above (THANKS), with the main difference of displaying UIImagePickerController's view as a subview of the current controller instead of as a subview of the main application window. Put this code in viewDidLoad():

    // Overlay view with custom camera controls and a top bar and a bottom bar
    self.overlay = [[CameraOverlayView alloc] initWithFrame:self.view.bounds];
    [self setOverlayViewModel];
    
    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    self.imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
    self.imagePicker.cameraFlashMode = UIImagePickerControllerCameraFlashModeOff;
    self.imagePicker.showsCameraControls = NO;
    self.imagePicker.navigationBarHidden = YES;
    self.imagePicker.toolbarHidden = YES;
    self.imagePicker.delegate = self;
    
    UIView *imagePickerView = self.imagePicker.view;
    
    if ([[UIScreen mainScreen] bounds].size.height == 568.0f) {
        // iPhone 5, 16:9 ratio, need to "zoom in" in order to fill the screen since there is extra space between top and bottom bars on a taller screen
        self.imagePicker.cameraViewTransform = CGAffineTransformScale(self.imagePicker.cameraViewTransform, 1.5, 1.5); // change 1.5 to suit your needs
    }
    CGRect cameraViewFrame = CGRectMake(0, self.overlay.topBarHeight, 
                                        self.view.bounds.size.width, 
                                        self.view.bounds.size.height - self.overlay.topBarHeight - self.overlay.bottomBarHeight);
    imagePickerView.frame = cameraViewFrame;
    
    // keep this order so that the overlay view is on top of the "live camera feed" view
    [self.view addSubview:imagePickerView];
    [self.view addSubview:self.overlay];
    

    TIP: When doing cameraViewTransform, make sure to apply the same transform on the resulting photo that the picker captures for you if you want the user to end up with the same image that they see through your transformed picker's view :P

提交回复
热议问题