I'm trying to use Key Value Observation to detect a change in the torchMode variable for an AVCaptureDevice. I'm having an issue where the switch statement doesn't recognize the value (which is an enum). Any idea whats going on? I know that the program gets to the line with the switch, then immediately skips all the cases. Here's the relevant code:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (context == CapturingStillImageContext)
{
BOOL isCapturingStillImage = [change[NSKeyValueChangeNewKey] boolValue];
if (isCapturingStillImage)
{
[self runStillImageCaptureAnimation];
}
}
else if (context == RecordingContext)
{
BOOL isRecording = [change[NSKeyValueChangeNewKey] boolValue];
dispatch_async(dispatch_get_main_queue(), ^{
if (isRecording)
{
[[self record] setTitle:NSLocalizedString(@"-", @"Recording button stop title") forState:UIControlStateNormal];
[[self record] setEnabled:YES];
}
else
{
[[self record] setTitle:NSLocalizedString(@"+", @"Recording button record title") forState:UIControlStateNormal];
[[self record] setEnabled:YES];
}
});
}
else if (context == SessionRunningAndDeviceAuthorizedContext)
{
BOOL isRunning = [change[NSKeyValueChangeNewKey] boolValue];
dispatch_async(dispatch_get_main_queue(), ^{
if (isRunning)
[[self record] setEnabled:YES];
else
[[self record] setEnabled:NO];
});
}
else if (context == TorchContext) {
id torchValue = [change objectForKey: NSKeyValueChangeNewKey];
NSString* title;
switch((AVCaptureTorchMode)torchValue) {
case AVCaptureTorchModeAuto: title = @"Auto"; break;
case AVCaptureTorchModeOff: title = @"Off"; break;
case AVCaptureTorchModeOn: title = @"On"; break;
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.torch setTitle:title forState:UIControlStateNormal];
//NSLog(self.torch);
});
}
else
{
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
Code for adding observer for key value:
- (void)viewWillAppear:(BOOL)animated
{
[self.navigationController setNavigationBarHidden:YES animated:NO];
dispatch_async([self sessionQueue], ^{
[self addObserver:self forKeyPath:@"sessionRunningAndDeviceAuthorized" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:SessionRunningAndDeviceAuthorizedContext];
[self addObserver:self forKeyPath:@"stillImageOutput.capturingStillImage" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:CapturingStillImageContext];
[self addObserver:self forKeyPath:@"movieFileOutput.recording" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:RecordingContext];
[self addObserver:self forKeyPath:@"videoDeviceInput.device.torchMode" options:(NSKeyValueObservingOptionNew) context:TorchContext];
...
}