UIImagePickerController shows black bar when zooming – Is this a bug in iOS?

我们两清 提交于 2019-12-06 13:39:25

I have found one way to work around what I for lack of better understanding would label a bug (as it happens with apple-provided samples as well).

The solution for me is to do manual zooming, by adding a UIPinchGestureRecognizer to the overlay view. The Controller must then implement a zooming callback which will get rid of the phenomenon described above.

@implementation CameraViewController
{
    CGFloat _lastScale; //< the current zoom scale before update
}

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.imagePicker = [[UIImagePickerController alloc] init];
    self.imagePicker.delegate = self;
    self.imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    self.imagePicker.allowsEditing = NO;
    self.imagePicker.showsCameraControls = NO;

    [[NSBundle mainBundle] loadNibNamed:@"CameraOverlay" owner:self options:nil];

    UIPinchGestureRecognizer *pinchRec = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(zoom:)];
    [self.overlayView addGestureRecognizer:pinchRec];

    self.imagePicker.cameraOverlayView = self.overlayView;  
    _lastScale = 1.;
}

- (void)zoom:(UIPinchGestureRecognizer *) sender
{
    // reset scale when pinch has ended so that future scalings are applied cumulatively and the zoom does not jump back (not sure I understand this)
    if([sender state] == UIGestureRecognizerStateEnded)
    {
        _lastScale = 1.0;
        return;
    }

    CGFloat scale = 1.0 - (_lastScale - sender.scale); // sender.scale gives current distance of fingers compared to initial distance. We want a value to scale the current transform with, so diff between previous scale and new scale is what must be used to stretch the current transform


    CGAffineTransform currentTransform = self.imagePicker.cameraViewTransform;
    CGAffineTransform newTransform = CGAffineTransformScale (currentTransform, scale, scale); // stretch current transform by amount given by sender

    newTransform.a = MAX(newTransform.a, 1.); // it should be impossible to make preview smaller than screen (or initial size)
    newTransform.d = MAX(newTransform.d, 1.);

    self.imagePicker.cameraViewTransform = newTransform;
    _lastScale = sender.scale;

}
@end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!