最后一节,我们为程序添加通过手势对摄像头进行缩放控制的功能。
添加实例变量,并在viewDidLoad方法的最后,进行初始化:
CGFloat _initialPinchZoom;
[_previewView addGestureRecognizer:[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchDetected:)]];
实现pinchDetected:方法:
- (void)pinchDetected:(UIPinchGestureRecognizer*)recogniser
{
// 1
if (!_videoDevice)
return;
// 2
if (recogniser.state == UIGestureRecognizerStateBegan)
{
_initialPinchZoom = _videoDevice.videoZoomFactor;
}
// 3
NSError *error = nil;
[_videoDevice lockForConfiguration:&error];
if (!error) {
CGFloat zoomFactor;
CGFloat scale = recogniser.scale;
if (scale < 1.0f) {
// 4
zoomFactor = _initialPinchZoom - pow(_videoDevice.activeFormat.videoMaxZoomFactor, 1.0f - reco

本文介绍了如何使用AV Foundation在iOS应用中实现摄像头的缩放控制功能,以便于二维码扫描。通过添加手势识别,记录初始缩放因子,根据用户手势调整摄像头的缩放级别,并限制缩放范围,最终实现平滑的缩放效果。
最低0.47元/天 解锁文章
3万+

被折叠的 条评论
为什么被折叠?



