问题描述
我正在尝试通过以下代码设置相机的缩放级别:
I'm trying to set the zoom level of a camera by this code:
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
if ([videoDevice lockForConfiguration:nil]) {
float newzoom=1.3;
videoDevice.videoZoomFactor = newzoom;
[videoDevice unlockForConfiguration];
}
此代码在ios 7中不起作用(适用于ios 9) ),它会导致异常:
This code doesn't not works in ios 7(it works in ios 9), it cause always an exception:
Terminating app due to uncaught exception 'NSRangeException', reason: 'videoZoomFactor out of range'
我无法找到信息,但ios 7中的变焦范围似乎是从1到2 。但是我试图为浮动newzoom设置的每个值都会导致异常...如何在Ios 7中设置videoZoomFactor?
I can't find information but the zoom range in ios 7 seems to be "from 1 to 2". But every value i have tried to set for the float newzoom cause the exception... How i can do to set the videoZoomFactor in Ios 7?
EDIT
我决定在设备不支持缩放时隐藏缩放按钮。所以这是我用过的代码:
I have decided to hide the zoom button when the device doesn't support the zoom. So this is the code i have used:
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表示设备不支持这种变焦。它似乎工作......有一个更好的方法来做这个检查?我在文档中找不到支持的设备列表...
If max and min are equals to 1 means that the device doesn't support this kind of zoom. It seems to work... There is a better way to do this check? I can't find a list of the supported devices in documentation...
推荐答案
根据苹果文档,如果设备的videoMaxZoomFactor是1,然后缩放不可用:
According to apple documentation, if the device's videoMaxZoomFactor is 1, then zoom is not available:
所以在你的情况下,你可以通过检查这个属性隐藏zoomButton:
So in your case, you could hide the zoomButton by just checking this property:
AVCaptureDevice *videoDevice = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
float max=videoDevice.activeFormat.videoMaxZoomFactor;
if (max==1) {
[ZoomButton setHidden:YES];
}
这篇关于AVCaptureDevice videoZoomFactor总是超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!