当视图中的内容发生更改时,在失去焦点后如何处理从特定POI处的“点击到焦点”自动切换回“自动焦点”状态?如果您注意到库存相机应用程序或UIImagePickerController中的对焦行为,则在您点击焦点某个区域并将手机移开后,相机可以自动切换到屏幕中心的连续自动对焦模式。
我需要比UIImagePickerController所能提供的更多的灵活性,所以我首先需要使用AVFoundation来模拟UIImagePickerController行为……
发布于 2013-05-31 02:34:24
一开始对我来说这听起来很复杂...但事情变得非常简单,苹果已经为我们完成了99%的工作。您需要做的就是在"AVCaptureDeviceSubjectAreaDidChangeNotification"!上设置"subjectAreaChangeMonitoringEnabled“并注册KVO在iOS 6.1文档中:
此属性的值指示接收器是否应监视视频主题区域的更改,如照明更改、大幅移动等。如果启用了主体区域变化监视,则每当捕获设备对象检测到主体区域的变化时,捕获设备对象就发送,此时感兴趣的客户端可能希望重新聚焦、调整曝光、白平衡等。
在更改此属性的值之前,必须调用lockForConfiguration:以获得对设备配置属性的独占访问权限。如果不这样做,则设置此属性的值将引发异常。完成设备配置后,调用unlockForConfiguration解除锁定,并允许其他设备配置设置。
您可以使用键值观察来观察此属性值的更改。
(更好的是,您不需要处理太多的情况。如果设备在POI处处于"adjustingFocus“的中间,并且内容发生了更改,该怎么办?您不希望设备退回到中心的自动对焦状态,而希望焦点操作完成。只有在焦点完成后才会触发“区域did更改通知”。)
我的项目中的一些示例代码片段。(其结构遵循官方的AVFoundation示例AVCam,因此您可以轻松地将其放入并试用):
// CameraCaptureManager.m
@property (nonatomic, strong) AVCaptureDevice *backFacingCamera;
- (id) init{
self = [super init];
if (self){
// TODO: more of your setup code for AVFoundation capture session
for (AVCaptureDevice *device in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
if (device.position == AVCaptureDevicePositionBack){
self.backFacingCamera = device;
}
}
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
void (^subjectAreaDidChangeBlock)(NSNotification *) = ^(NSNotification *notification) {
if (self.videoInput.device.focusMode == AVCaptureFocusModeLocked ){
// All you need to do is set the continuous focus at the center. This is the same behavior as
// in the stock Camera app
[self continuousFocusAtPoint:CGPointMake(.5f, .5f)];
}
};
self.subjectAreaDidChangeObserver = [notificationCenter addObserverForName:AVCaptureDeviceSubjectAreaDidChangeNotification
object:nil
queue:nil
usingBlock:subjectAreaDidChangeBlock];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[self addObserver:self forKeyPath:keyPathAdjustingFocus options:NSKeyValueObservingOptionNew context:NULL];
}
return self;
}
-(void) dealloc{
// Remove the observer when done
NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
[notificationCenter removeObserver:self.deviceOrientationDidChangeObserver];
}
- (BOOL) setupSession{
BOOL sucess = NO;
if ([self.backFacingCamera lockForConfiguration:nil]){
// Turn on subject area change monitoring
self.backFacingCamera.subjectAreaChangeMonitoringEnabled = YES;
}
[self.backFacingCamera unlockForConfiguration];
// TODO: Setup add input etc...
return sucess;
}发布于 2015-06-16 20:19:58
我刚刚看到了@小朝阳的回复评论,我想补充一下代码CGPointMake(.5f, .5f)的解释,根据苹果的接口,您设置摄像头的CGPoint在{0,0}到{1,1}的范围内,同时CGPointMake(.5f, .5f)表示摄像头的中心。
此属性表示CGPoint,其中{0,0}对应于图片区域的左上角,{1,1}对应于横向模式下的右下角,主页按钮位于右侧-即使设备处于纵向模式,此属性也适用
https://stackoverflow.com/questions/16843512
复制相似问题