Search code examples
iosios7avfoundationavcapturedevice

AVCaptureDevice videoZoomFactor always Out of Range


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...


Solution

  • 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];
    }