gpt4 book ai didi

iOS扫描二维码实现手势拉近拉远镜头

转载 作者:qq735679552 更新时间:2022-09-28 22:32:09 24 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章iOS扫描二维码实现手势拉近拉远镜头由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

在做扫码需求,往往会有放大镜头需求.

苹果提供了AVCaptureConnection中,videoScaleAndCropFactor:缩放裁剪系数,使用该属性,可以实现拉近拉远镜头。再结合手势UIPinchGestureRecognizer,就很简单实现手势拉近拉远镜头.

手势代码 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
///记录开始的缩放比例
@property(nonatomic,assign)CGFloat beginGestureScale;
 
///最后的缩放比例
@property(nonatomic,assign)CGFloat effectiveScale;
 
- ( void )cameraInitOver
{
  if (self.isVideoZoom) {
   UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchDetected:)];
   pinch.delegate = self;
   [self.view addGestureRecognizer:pinch];
  }
}
 
- ( void )pinchDetected:(UIPinchGestureRecognizer*)recogniser
{
  self.effectiveScale = self.beginGestureScale * recogniser.scale;
  if (self.effectiveScale < 1.0){
   self.effectiveScale = 1.0;
  }
  [self.scanObj setVideoScale:self.effectiveScale];
 
}
 
- ( BOOL )gestureRecognizerShouldBegin:(UIGestureRecognizer *)gestureRecognizer
{
  if ( [gestureRecognizer isKindOfClass:[UIPinchGestureRecognizer class ]] ) {
   _beginGestureScale = _effectiveScale;
  }
  return YES;
}

拉近拉远镜头代码 。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
- ( void )setVideoScale:(CGFloat)scale
{
  [_input.device lockForConfiguration:nil];
 
  AVCaptureConnection *videoConnection = [self connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self stillImageOutput] connections]];
  CGFloat maxScaleAndCropFactor = ([[self.stillImageOutput connectionWithMediaType:AVMediaTypeVideo] videoMaxScaleAndCropFactor])/16;
 
  if (scale > maxScaleAndCropFactor)
   scale = maxScaleAndCropFactor;
 
  CGFloat zoom = scale / videoConnection.videoScaleAndCropFactor;
 
  videoConnection.videoScaleAndCropFactor = scale;
 
  [_input.device unlockForConfiguration];
 
  CGAffineTransform transform = _videoPreView.transform;
  [CATransaction begin];
  [CATransaction setAnimationDuration:.025];
 
   _videoPreView.transform = CGAffineTransformScale(transform, zoom, zoom);
 
  [CATransaction commit];
 
}

有一点需要注意:the videoScaleAndCropFactor property may be set to a value in the range of 1.0 to videoMaxScaleAndCropFactor,videoScaleAndCropFactor这个属性取值范围是1.0-videoMaxScaleAndCropFactor,如果你设置超出范围会崩溃哦! 。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我.

原文链接:https://blog.csdn.net/sinat_30336277/article/details/79276113 。

最后此篇关于iOS扫描二维码实现手势拉近拉远镜头的文章就讲到这里了,如果你想了解更多关于iOS扫描二维码实现手势拉近拉远镜头的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com