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

标题: ios - AVCaptureDevice videoZoomFactor 总是超出范围 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 09:29
标题: ios - AVCaptureDevice videoZoomFactor 总是超出范围

我正在尝试通过此代码设置相机的缩放级别:

 AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];


 if ([videoDevice lockForConfiguration:nil]) {
     float newzoom=1.3;
     videoDevice.videoZoomFactor = newzoom;
     [videoDevice unlockForConfiguration];
 }

此代码在 ios 7 中不起作用(它在 ios 9 中起作用),它总是会导致异常:

Terminating app due to uncaught exception 'NSRangeException', reason: 'videoZoomFactor out of range'

我找不到信息,但 ios 7 中的缩放范围似乎是“从 1 到 2”。但是我尝试为 float newzoom 设置的每个值都会导致异常...如何在 Ios 7 中设置 videoZoomFactor?

编辑

我决定在设备不支持缩放时隐藏缩放按钮。所以这是我使用的代码:

AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
float max=videoDevice.activeFormat.videoMaxZoomFactor;
float min=MIN(videoDevice.activeFormat.videoMaxZoomFactor, 4.0f);


if (max==1 && min==1) {

    [ZoomButton setHidden:YES];
}

如果 max 和 min 等于 1 表示设备不支持这种缩放。它似乎工作......有更好的方法来做这个检查吗?我在文档中找不到受支持设备的列表...



Best Answer-推荐答案


根据苹果文档,如果设备的 videoMaxZoomFactor 为 1,则缩放不可用:

If the device's videoZoomFactor property is assigned a larger value, an NSRangeException will be thrown. A maximum zoom factor of 1 indicates no zoom is available.

所以在你的情况下,你可以通过检查这个属性来隐藏 zoomButton:

AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
float max=videoDevice.activeFormat.videoMaxZoomFactor;

if (max==1) {
    [ZoomButton setHidden:YES];
}

关于ios - AVCaptureDevice videoZoomFactor 总是超出范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33276013/






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