问题
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];
}
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'
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];
}
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...
回答1:
According to apple documentation, if the device's videoMaxZoomFactor is 1, then zoom is not available:
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.
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];
}
回答2:
You should check videoMaxZoomFactor
before setting videoZoomFactor
because videoZoomFactor
is NOT always "from 1.0 to 2.0" .
Docs:
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 therampToVideoZoomFactor:withRate:
method.
来源:https://stackoverflow.com/questions/33276013/avcapturedevice-videozoomfactor-always-out-of-range