Focusing iOS Camera on Tap Location

时间:2015-05-12 23:02:41

标签: ios iphone swift

I've got a UIGesture recognizer that calls the below function when the screen is tapped.

The device is in portrait mode and the "Exception in thread "main" java.lang.ClassCastException: net.sf.json.JSONObject cannot be cast to java.lang.String" takes up a variable height between about 50% and 90% of the screen, but that shouldn't matter because I'm getting the tap location within that view.

The apple docs state the focal point is a normalized coordinate (x and y are both between 0 and 1) with the top left in landscape mode (with the home button to the right) at (0, 0) and bottom right at (1, 1).

Setting the focus mode to AutoFocus rather than ContinuousAutoFocus made things worse. What am I missing? Other apps have this working just fine.

cameraView

Attempted this as well:

func focusPhoto(recognizer : UITapGestureRecognizer) {
    if photoCaptureDevice.lockForConfiguration(nil) && photoCaptureDevice.focusPointOfInterestSupported {
        var tapLocation = recognizer.locationInView(cameraView);
        var focalPoint = CGPoint(x: tapLocation.x/cameraView.frame.width, y: tapLocation.y/cameraView.frame.height);

        photoCaptureDevice.focusPointOfInterest = focalPoint
        //photoCaptureDevice.focusMode = AVCaptureFocusMode.AutoFocus

        photoCaptureDevice.unlockForConfiguration()
    }
}

1 个答案:

答案 0 :(得分:1)

您使用的是AVCaptureVideoPreviewLayer吗?

您可以使用一种方法将点击点转换为正确的坐标空间进行聚焦。

- captureDevicePointOfInterestForPoint: