简体   繁体   中英

Crash while getting the current Autofocus system in ios

There are two types of focus detection right from iphone 6 introduction, 1. Contrast detection 2. Phase detection

from iphone 6.6+ it uses phase detection.

I am trying to get the current focus system

self.format = [[AVCaptureDeviceFormat alloc] init];
[self.currentDevice setActiveFormat:self.format];

AVCaptureAutoFocusSystem currentSystem = [self.format autoFocusSystem];
if (currentSystem == AVCaptureAutoFocusSystemPhaseDetection)
{
     [self.currentDevice addObserver:self forKeyPath:@"lensPosition"    options:NSKeyValueObservingOptionNew context:nil];
}
else if(currentSystem == AVCaptureAutoFocusSystemContrastDetection)
{
  [self.currentDevice addObserver:self forKeyPath:@"adjustingFocus" options:NSKeyValueObservingOptionNew context:nil];
}
else{
    NSLog(@"No observers added");
}

but now its crashing in the following line

AVCaptureAutoFocusSystem currentSystem = [self.format autoFocusSystem];

I am unable to find proper description for the crash.

You're creating some " AVCaptureDeviceFormat ", but nothing is actually set up by default with it. It's just unusable garbage (and that's why you are getting a crash).

Each capture device has one or two formats it can work with.

You can see them by doing something like:

for (AVCaptureDeviceFormat *format in [self.currentDevice formats]) {
       CFStringRef formatName = CMFormatDescriptionGetExtension([format formatDescription], kCMFormatDescriptionExtension_FormatName);
       NSLog(@"format name is %@", (NSString *)formatName);
}

What you should be doing is deciding which AVCaptureDevice you want to use (eg the front camera, the back camera, whatever), getting the format from that , and then setting [self.currentDevice setActiveFormat:self.format] ".

The WWDC 2013 session "What's New in Camera Capture" video has more information on how to do this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM