1

I am trying to implement pinch zoom in/out on camera view.

I have this code so far:

- (void) setZoom:(float)zoomValue{

NSError *error = nil;
NSLog(@"Current zoom level %f  " , zoomValue);
if([_device lockForConfiguration:&error]){
    if(zoomValue > 1 ){
        if(zoomValue < _device.activeFormat.videoMaxZoomFactor){
            _device.videoZoomFactor = zoomValue;

            [_device unlockForConfiguration];
        }else{
            _device.videoZoomFactor = _device.activeFormat.videoMaxZoomFactor;
            //self.currentZoom = _device.activeFormat.videoMaxZoomFactor;
            [_device unlockForConfiguration];

        }
    }else if(zoomValue < 1){
        _device.videoZoomFactor = 1;
        //self.currentZoom = 1;
        [_device unlockForConfiguration];
    }
}

}

And this is the code of the pinch gesture handler:

-(IBAction)handlePinchWithGestureRecognizer:(UIPinchGestureRecognizer *)pinchGestureRecognizer {

    if ( pinchGestureRecognizer.state == UIGestureRecognizerStateBegan )
    {
        _zoomGestureCurrentZoom = pinchGestureRecognizer.scale;
    }
    else if ( pinchGestureRecognizer.state == UIGestureRecognizerStateChanged )
    {
        // we have to jump through some hoops to clamp the scale in a way that makes the UX intuitive
        float scaleDeltaFactor = pinchGestureRecognizer.scale/_zoomGestureLastScale;
        float currentZoom = self.zoomGestureCurrentZoom;
        float newZoom = currentZoom * scaleDeltaFactor;
        // clamp
        float kMaxZoom = 6.0f;
        float kMinZoom = 1.0f;
        newZoom = MAX(kMinZoom,MIN(newZoom,kMaxZoom));
        //self.view.transform = CGAffineTransformScale([[gestureRecognizer view] transform], newZoom, newZoom);
        [_imageCameraSource setZoom:newZoom];
        // store for next time
        self.zoomGestureCurrentZoom = newZoom;
     }
}

There are few problems i am having:

  1. From time to time the camera will just freeze, which i assume it is because of invalid zoom value passed, but can't figure out.
  2. Every time i start the pinch gesture, it starts from normal size, and then increases, so the zoom out functionality is not really working.

If anyone has any working code, it will be good to take a look and figure out the whole functionality or any other suggestion will be welcome.

Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Borce Ivanovski
  • 561
  • 1
  • 4
  • 23

0 Answers0