AVCaptureDevice videoZoomFactor

时间:2014-01-16 18:27:01

标签: ios avfoundation avcapturedevice

我正在使用AVFoundation/AVFoundation.h使用相机进行项目。现在,我想实现缩放屏幕。

我检查了AVCaptureDevice上的文档,找到了一个名为“videoZoomFactor”的参数,该参数表示它可用于定义缩放率。

因此,我使用了以下代码:

device.videoZoomFactor = device.activeFormat.videoZoomFactorUpscaleThreshold;

但该应用程序崩溃并显示「videoZoomFactor超出范围。」   这怎么发生的?我该怎么做才能放大相机?

3 个答案:

答案 0 :(得分:2)

它适用于我,在以下代码段中:

if ([device respondsToSelector:@selector(setVideoZoomFactor:)]) {
    if ([ device lockForConfiguration:nil]) {
        float zoomFactor = device.activeFormat.videoZoomFactorUpscaleThreshold;
        [device setVideoZoomFactor:zoomFactor];
        [device unlockForConfiguration];
    }
}

尝试记录device.activeFormat.videoZoomFactorUpscaleThreshold并查看实际值是什么。

答案 1 :(得分:0)

你需要首先检查sessionPreset,使用 - (BOOL)supportsAVaptureSessionPreset:(NSString *)预设

另外device.videoZoomFactor范围应介于1.0和2.0之间,因此在进行设置之前先检查范围。

答案 2 :(得分:0)

// in class
CGFloat zoomBegin;
CGFloat zoomMax;

// on create video connection
NSError *error;
if( YES == [camDevice lockForConfiguration:&error] )
{
    // A maximum zoom factor of 1 indicates no zoom is available.
    zoomMax = camDevice.activeFormat.videoMaxZoomFactor;
    [camDevice unlockForConfiguration];
}
[self.view addGestureRecognizer:[[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchToZoom:)]];

- (void)pinchToZoom:(UIPinchGestureRecognizer*)gesture
{
    switch (gesture.state)
    {
        case UIGestureRecognizerStateBegan:
        {
            zoomBegin = camDevice.videoZoomFactor;
            NSLog(@"zoom begin:%.3f", zoomBegin);
        }break;
        case UIGestureRecognizerStateChanged:
        {
            CGFloat zoomTo = zoomBegin + (gesture.scale * 2 - 2);
            // step 0.01 between 1.0 and zoomMax (4x on iPhone 6s)
            int msc = (int)(((zoomTo+0.001)*100))%100;
            zoomTo = (NSInteger)zoomTo + msc * 0.01;
            zoomTo = fmaxf(1, fminf(zoomTo, zoomMax));

            if ( camDevice.videoZoomFactor != zoomTo )
            {
                dispatch_async(sessionQueue, ^{
                    NSError *error;
                    if ( YES == [camDevice lockForConfiguration:&error] )
                    {
                        camDevice.videoZoomFactor = zoomTo;
                        [camDevice unlockForConfiguration];
                        NSLog(@"zoom changed:%.3f", zoomTo);
                    }
                });
            }
        }break;
        default:
            break;
    }
}