Iphone AVFoundation-如何控制接触
点击拍照后,我想锁定曝光,并在曝光不再调整时关闭手电筒。因此,我添加了一个观察者来处理调整曝光:Iphone AVFoundation-如何控制接触,iphone,objective-c,avfoundation,Iphone,Objective C,Avfoundation,点击拍照后,我想锁定曝光,并在曝光不再调整时关闭手电筒。因此,我添加了一个观察者来处理调整曝光: - (IBAction)configureImageCapture:(id)sender { [self.session beginConfiguration]; [self.cameraController device:self.inputDevice exposureMode:AVCaptureExposureModeAutoExpose]; [self.camera
- (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发布了这个,这正是我想要做的
但是我的照片是在火炬熄灭之前拍的
这是我的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");
我用闪光灯而不是手电筒得到了类似的结果。我也有一个“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];
但是,如果您需要在较低曝光率下拍摄多张不带闪光灯的照片,此策略可能不起作用。这几乎肯定是一个时间问题。在
if
块内调用captureStillImageAsynchronouslyFromConnection:completionHandler:
。然后,将始终在曝光锁定后执行捕获
if ([self.inputDevice isExposureModeSupported:AVCaptureExposureModeLocked]) {
dispatch_async(dispatch_get_main_queue(),
^{
NSError *error = nil;
if ([self.inputDevice lockForConfiguration:&error]) {
//code to lock exposure here
//take photo here
}
});
}
好啊我知道这个问题是关于焦点和曝光的,但我会先接受曝光。我在这里找到了一个关于曝光的答案:我很难让KVO按预期运行。这张照片是在火炬关闭前拍摄的。我已经将代码更新到现在的状态。这里需要帮助。提前谢谢。您在哪里调用
captureStillImageAsynchronouslyFromConnection:completionHandler:
?可能在执行块之前调用它?@Pranav-我已经添加了captureStillImageAsynchronouslyFromConnection:completionHandler:method。我认为这可能是一个计时问题,或者我没有正确设置相机配置。我没有进入else if(context==FlashModeChangedContext)条件。相机是否会通过简单地添加一个与其他人类似的观察者(即[(AVCaptureDevice*)self.inputDevice addObserver:self forKeyPath:@“Adjusting Flash”选项:NSKeyValueObservingOptionNew context:MyAdjusting FlashObservationContext];)???是的,没错,但关键路径是flashMode
,看,我终于明白了你的最后一句话:然而,如果你需要在低曝光率下拍摄多张没有闪光灯的照片,这个策略可能不起作用。随着每次点击,拍摄的照片数量呈指数级增长。我不知道从哪里可以得到“指数级增长”。我想说的很简单:我的代码甚至没有试图锁定相机的曝光水平。它只是为一张有闪光灯的照片设置曝光水平,然后在曝光水平有机会调整之前拍摄一张没有闪光灯的照片。拍摄完这张照片后,iPhone会适当地调整曝光,使其更适合于无闪光灯照片,因此在调整完成之前,您可能只有一张或两张照片的时间。当我进入“观察者”时,我已经从Connection:completionHandler方法异步调用了captureStillImageAsynchronouslyFromConnection。我不知道如何执行您的建议,而且(我的一生)也找不到在调用captureStillImageAsynchronouslyFromConnection:completionHandler方法之前使用完成处理程序的代码示例。