3

我使用下面的代码来聚焦 iPhone 相机。但它不起作用。我从 Apple 的 AVCam 示例代码中获取此代码。我做错什么了吗?有什么方法可以检测 iPhone 是否对焦?

-(void) focusAtPoint:(CGPoint)point   
{
    AVCaptureDevice *device =  [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];;

    if (device != nil) {            
        if ([device isFocusPointOfInterestSupported] && [device isFocusModeSupported:AVCaptureFocusModeAutoFocus]) {

            NSError *error;

            if ([device lockForConfiguration:&error]) {             
                [device setFocusPointOfInterest:point];             
                [device setFocusMode:AVCaptureFocusModeAutoFocus];              
                [device unlockForConfiguration];                
            } else {                
                NSLog(@"Error in Focus Mode");
            }        

        }
    }
}
4

1 回答 1

7

for[AVCaptureDevice setFocusPointOfInterest:]的点与视图的触摸点不同,而是介于 0,0 和 1,1 之间的点(请参阅文档)。所以你必须这样做:

CGPoint point = [touch locationInView:self.cameraView];
point.x /= self.cameraView.frame.size.width;
point.y /= self.cameraView.frame.size.height;
[self focusAtPoint:point];
于 2012-05-04T13:55:30.637 回答