我正在开发一个作为视图控制器,管理器和处理器的相机应用程序。管理员基本上有AVCaptureSession
,AVCaptureDeviceInput
和AVCaptureStillImageOutput
到控制图像捕获。由于处理器执行繁重的图像处理操作,因此每次捕获图像时都会释放管理器以避免应用程序崩溃。
我还有一个按钮,可以按照Apple here的说明切换automaticallyEnablesLowLightBoostWhenAvailable
(低光设置)。这是切换功能:
- (IBAction)toggleLLBoost:(id)sender {
if([manager isLLBoostActivated]){
[self turnOffLLBoost];
} else {
[self turnOnLLBoost];
}
}
- (void)turnOffLLBoost
{
NSLog(@"LLBoost off");
[boostBt setSelected:NO];
[manager deactivateLLBoostMode];
[[[manager videoInput] device] removeObserver:self forKeyPath:@"lowLightBoostEnabled"];
}
- (void)turnOnLLBoost
{
NSLog(@"LLBoost on");
[boostBt setSelected:YES];
[manager activateLLBoostMode];
[[[manager videoInput] device] addObserver:self forKeyPath:@"lowLightBoostEnabled" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:NULL];
}
当低光设置为ON时,它将注册观察者,并在关闭时删除观察者。以下是管理器中为设置激活/取消激活的代码:
-(void)activateLLBoostMode
{
AVCaptureDevice *device = [videoInput device];
if([device isLowLightBoostSupported]){
if([device lockForConfiguration:NULL]){
[device setAutomaticallyEnablesLowLightBoostWhenAvailable:YES];
[device unlockForConfiguration];
[self setIsLLBoostActivated:YES];
NSLog(@"Low Light Boost is set? %hhd",[device automaticallyEnablesLowLightBoostWhenAvailable]);
}
}
}
-(void)deactivateLLBoostMode
{
AVCaptureDevice *device = [videoInput device];
if([device isLowLightBoostSupported]){
if([device lockForConfiguration:NULL]){
[device setAutomaticallyEnablesLowLightBoostWhenAvailable:NO];
[device unlockForConfiguration];
[self setIsLLBoostActivated:NO];
NSLog(@"Low Light Boost is set? %hhd",[device automaticallyEnablesLowLightBoostWhenAvailable]);
}
}
}
一开始,一切都会完美无缺。当lowLightBoostEnabled
更改值时,将调用观察者。但是,在我释放管理器(会话,设备,输出等)然后在完成图像处理后再次重新设置管理器之后,观察者永远不会被调用。尽管'automaticEnablesLowLightBoostWhenAvailable'已被设置为YES。
任何建议或建议为什么会发生这种情况?
附加信息:
这是经理如何初始化的代码片段。在调用alloc / init setupSession
之后。在发布时,将调用dealloc方法:
-(BOOL)setupSession
{
[self deactivateLLBoostMode];
[self setIsLLBoostActivated:NO];
//Setting Session
[self setSession:[AVCaptureSession new]];
session.sessionPreset = AVCaptureSessionPresetPhoto;
//Setting Input
[self setVideoInput:[[AVCaptureDeviceInput alloc] initWithDevice:[self backFacingCamera] error:nil]];
if ([session canAddInput:videoInput]) {
[session addInput:videoInput];
}
//Setting Output
[self setStillImage:[AVCaptureStillImageOutput new]];
if ([stillImage isStillImageStabilizationSupported]) {
[stillImage setAutomaticallyEnablesStillImageStabilizationWhenAvailable:YES];
}
[stillImage setOutputSettings:@{AVVideoCodecKey: AVVideoCodecJPEG, AVVideoQualityKey:@0.6}];
if ([session canAddOutput:stillImage]) {
[session addOutput:stillImage];
}
return true;
}
-(void)dealloc
{
[session stopRunning];
self.session = nil;
self.stillImage = nil;
self.videoInput = nil;
self.toSaveImage = nil;
self.rawImages = nil;
}