AVCaptureDevice videoZoomFactor 总是超出范围

AVCaptureDevice videoZoomFactor always Out of Range

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

 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 中的缩放范围似乎是 "from 1 to 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表示设备不支持这种缩放。它似乎工作......有更好的方法来做这个检查吗?我在文档中找不到支持的设备列表...

您应该在设置 videoZoomFactor 之前检查 videoMaxZoomFactor 因为 videoZoomFactor 并不总是 "from 1.0 to 2.0" 。 文档:

This value is a multiplier. For example, a value of 2.0 doubles the size of an image’s subject (and halves the field of view). Allowed values range from 1.0 (full field of view) to the value of the active format’s videoMaxZoomFactor property. Setting the value of this property jumps immediately to the new zoom factor. For a smooth transition, use the rampToVideoZoomFactor:withRate: method.

根据苹果文档,如果设备的 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];
}