在做扫码需求,往往会有放大镜头需求。 苹果提供了AVCaptureConnection中,videoScaleAndCropFactor:缩放裁剪系数,使用该属性,可以实现拉近拉
在做扫码需求,往往会有放大镜头需求。
苹果提供了AVCaptureConnection中,videoScaleAndCropFactor:缩放裁剪系数,使用该属性,可以实现拉近拉远镜头。再结合手势UIPinchGestureRecognizer,就很简单实现手势拉近拉远镜头。
手势代码
///记录开始的缩放比例
@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;
}
拉近拉远镜头代码
- (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,如果你设置超出范围会崩溃哦!
--结束END--
本文标题: iOS扫描二维码实现手势拉近拉远镜头
本文链接: https://www.lsjlt.com/news/30338.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0