AFTER點擊拍照時,我想鎖定曝光並在曝光不再調整時關閉手電筒。所以,我添加了一個觀察者來處理adjustingExposure:AVFoundation - 如何控制曝光
- (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.captureConnection completionHandler。該塊在拍攝圖像後發生。 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");
好的。我知道這個問題是關於焦點和曝光,但我會首先解決曝光問題。我找到了一個答案,我試圖用曝光在這裏:http://stackoverflow.com/questions/12635446/accessing-ios-6-new-apis-for-camera-exposure-and-shutter-speed/20660981 #20660981 – Patricia
我無法讓KVO按預期執行。在割炬關閉之前拍攝圖像。 – Patricia
我已經更新了我現在擁有的代碼。在這裏尋找幫助。先謝謝你。 – Patricia