OGeek|极客世界-中国程序员成长平台

标题: ios - 使用Objective-C放大和缩小相机 View ? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 13:13
标题: ios - 使用Objective-C放大和缩小相机 View ?

如何使用 UIPinchGestureRecognizer 和 AVCaptureDevice 应用放大和缩小相机 View 。 它没有正确缩放。
缩小在我的以下代码中不起作用:

 AVCaptureDevice *device =[AVCaptureDevice defaultDeviceWithMediaType:
                                                       AVMediaTypeVideo];
 AVCaptureDeviceFormat *format = device.activeFormat;
 CGFloat maxZoomFactor = format.videoMaxZoomFactor;
 const CGFloat pinchVelocityDividerFactor = 2.0f;

 if (pinchRecognizer.state == UIGestureRecognizerStateChanged || pinchRecognizer.state ==UIGestureRecognizerStateBegan)
 {
     NSError *error = nil;
      if ([device lockForConfiguration:&error])
      {
         CGFloat desiredZoomFactor = device.videoZoomFactor +
             atan2f(pinchRecognizer.velocity, pinchVelocityDividerFactor);

          // Check if desiredZoomFactor fits required range from 1.0
                                   //to activeFormat.videoMaxZoomFactor
          if((1.0 <= pinchRecognizer.scale) && (2.5 >= pinchRecognizer.scale))
          device.videoZoomFactor = pinchRecognizer.scale * pinchRecognizer.scale;  

        [device unlockForConfiguration];
      }
      else
      {
        NSLog(@"error: %@", error);
      }
 }  
}

请帮助任何人。
谢谢



Best Answer-推荐答案


//捏手势识别器方法定义

- (void)handlePinchZoomUIPinchGestureRecognizer *)pinchRecognizer
 {
     AVCaptureDevice *device =[AVCaptureDevicedefaultDeviceWithMediaType:
                                                       AVMediaTypeVideo];
     AVCaptureDeviceFormat *format = device.activeFormat;
     CGFloat maxZoomFactor = format.videoMaxZoomFactor;
     NSArray *formats = device.formats;
     const CGFloat pinchVelocityDividerFactor = 2.0f;
     if (pinchRecognizer.state == UIGestureRecognizerStateChanged || pinchRecognizer.state ==UIGestureRecognizerStateBegan)
     {
        NSError *error = nil;
        if ([device lockForConfiguration:&error])
        {
           CGFloat desiredZoomFactor = device.videoZoomFactor +
           atan2f(pinchRecognizer.velocity, pinchVelocityDividerFactor);

           device.videoZoomFactor =   MAX(1.0, MIN(desiredZoomFactor,
                                          device.activeFormat.videoMaxZoomFactor));
           [device unlockForConfiguration];
        } 
       else
       {
         NSLog(@"error: %@", error);
       }
     }
  }

关于ios - 使用Objective-C放大和缩小相机 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35841390/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4