2

I'm trying to set point of focus with front camera.

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

    let screenSize = cameraView.bounds.size

    let frameSize:CGSize = view.frame.size

    if let touchPoint = touches.first {

        var location:CGPoint = touchPoint.locationInView(cameraView)

        print("Tap Location : X: \(location.x), Y: \(location.y)")

        if cameraManager.cameraDevice == .Front {
            print("Front camera is used")

            location.x = frameSize.width - location.x;
        }
        else {
            print("Back camera is used")
        }

        let x = location.x / frameSize.width
        let y = 1.0 - (location.x / frameSize.width)

        let focusPoint = CGPoint(x: x, y: y)

        print("POINT : X: \(x), Y: \(y)")


        let captureDevice = (AVCaptureDevice.devicesWithMediaType(AVMediaTypeVideo) as! [AVCaptureDevice]).filter{$0.position == .Front}.first

        if let device = captureDevice {
            do {
                try device.lockForConfiguration()

                let support:Bool = device.focusPointOfInterestSupported

                if support  {

                    print("focusPointOfInterestSupported: \(support)")

                    device.focusPointOfInterest = focusPoint

                    // device.focusMode = .ContinuousAutoFocus
                    device.focusMode = .AutoFocus
                    // device.focusMode = .Locked

                    device.unlockForConfiguration()

                    print("Focus point was set successfully")
                }
                else{
                    print("focusPointOfInterestSupported is not supported: \(support)")
                }
            }
            catch {
                // just ignore
                print("Focus point error")
            }
        }
    }
}

It works on iphone6 for back-camera. But, when I activate the front camera, it threshed. focusPointOfInterestSupported is always returns false.

How can I solve this problem. Can I can set a focus point programmatically?

Thanks!

gkhanacer
  • 575
  • 7
  • 25

1 Answers1

5

The focusPointOfInterestSupported boolean is indicating that the front camera of this device doesn't support setting the focus this way. In fact, it appears that the "FaceTime" camera of the iPhone 6 has a fixed focus. There is no mention of "focus" in the Tech Specs section of the iPhone 6 "FaceTime" camera, but there is in the rear-facing "iSight" camera section. Additionally, trying to refocus the front-facing camera on a blurry object close to the lens (by tapping in the Camera app) appears to only adjust the exposure, and not the focus.

Source: https://developer.apple.com/library/ios/documentation/AVFoundation/Reference/AVCaptureDevice_Class/index.html#//apple_ref/occ/instp/AVCaptureDevice/focusPointOfInterestSupported

Source: http://www.apple.com/iphone-6/specs/

Sam Falconer
  • 488
  • 4
  • 9
  • Thanks for reply. You are right. The front camera supports only adjusting the exposure. Do you know, what about zoom? I can't set front camera zoom coefficient by avfoundation. But some applications which are at apple store can set zoom. – gkhanacer Apr 19 '16 at 12:56
  • Neither the front or back camera of the iPhone support optical zoom. All "zooming" that apps do is effectively just cropping the image to make it look zoomed. If AVFoundation is preventing you from using its "zoom" on the front camera, it may be because it has a lower resolution than the rear camera. The Camera app also doesn't allow zooming the front camera, as it does with the rear camera. – Sam Falconer Apr 19 '16 at 13:48