之后,点击拍照,我想锁定曝光并在不再调整曝光后立即关闭手电筒。因此,我添加了一个观察者来处理adjustmentExposure:
- (IBAction)configureImageCapture:(id)sender
{
[self.session beginConfiguration];
[self.cameraController device:self.inputDevice exposureMode:AVCaptureExposureModeAutoExpose];
[self.cameraController device:self.inputDevice torchMode:AVCaptureTorchModeOn torchLevel:0.8f];
[self.session commitConfiguration];
[(AVCaptureDevice *)self.inputDevice addObserver:self forKeyPath:@"adjustingExposure" options:NSKeyValueObservingOptionNew context:MyAdjustingExposureObservationContext];
}
这是observeValueForKeyPath方法:
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if (context == MyAdjustingExposureObservationContext) {
if( [keyPath isEqualToString:@"adjustingExposure"] )
{
BOOL adjustingExposure = [ [change objectForKey:NSKeyValueChangeNewKey] isEqualToNumber:[NSNumber numberWithInt:1] ];
if (!adjustingExposure)
{
[(AVCaptureDevice *)self.cameraController.inputDevice removeObserver:self forKeyPath:@"adjustingExposure"];
if ([self.inputDevice isExposureModeSupported:AVCaptureExposureModeLocked]) {
dispatch_async(dispatch_get_main_queue(),
^{
NSError *error = nil;
if ([self.inputDevice lockForConfiguration:&error]) {
// 5) lock the exposure
[self.cameraController device:self.inputDevice exposureMode:AVCaptureExposureModeLocked];
// 6) turn off the Torch
[self.cameraController device:self.inputDevice torchMode:AVCaptureTorchModeOn torchLevel:0.0001f];
[self.inputDevice unlockForConfiguration];
}
});
}
}
}
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
@ user3115647发布了这个information,这正是我想要做的。
但是我的照片是拍摄的。在之前,割炬已关闭。
这是我的captureStillImageAsynchronouslyFromConnection:self.captureConnectioncompleteHandler。拍摄图像后发生此块。假定在拍摄图像之前相机调整曝光时,observeValueForKeyPath会发生。但是在拍摄图像之前,我的手电筒并没有变低。这是时间问题,或者我没有正确设置相机配置。
- (void)captureImage
{
// configureImageCapture has already been done
[self.stillImageOutput captureStillImageAsynchronouslyFromConnection:self.captureConnection completionHandler: ^(CMSampleBufferRef imageSampleBuffer, NSError *error)
{
if (imageSampleBuffer != NULL)
{
// Log the image properties
CFDictionaryRef attachmentsRef = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
NSDictionary *properties = (__bridge NSDictionary *)(attachmentsRef);
NSLog(@"Image Properties => %@", (properties.count) ? properties : @"none");
最佳答案
通过使用闪光灯而不是手电筒,我得到了类似的结果。我也有@"videoDevice.flashActive"
的观察者。我确实尝试过先使用exposureModeLocked
,但对我也不起作用。
下面的代码可能不仅仅可以单独工作,还可以从我的工作中简化出来。
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void*)context
{
if (context == AdjustingExposureContext)
{
self.isAdjustingExposure = [change[NSKeyValueChangeNewKey] boolValue];
}
else if (context == FlashModeChangedContext)
{
self.isFlashActive = [change[NSKeyValueChangeNewKey] boolValue];
if (!self.flashActive)
{
[self captureImage]; // QUICKLY! capture 2nd image without
} // flash before exposure adjusts
}
if (!self.isAdjustingExposure && self.flashActive)
{
[self removeObserver:self forKeyPath:@"videoDevice.adjustingExposure" context:AdjustingExposureContext];
[self captureImage]; // capture 1st image with the flash on
}
}
现在在
captureStillImageAsynchronouslyFromConnection:
的回调中,if (self.isFlashActive)
[self.videoDeviceInput.device setFlashMode:NO];
但是,如果您需要以较低的曝光率拍摄多张不带闪光灯的照片,则此策略可能不起作用。
关于iphone - AVFoundation-如何控制曝光,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22029381/