4

You can tell the iPhone camera to focus on near or far objects by doing:

try! device.lockForConfiguration()
defer { device.unlockForConfiguration() }
device.autoFocusRangeRestriction = .near

But whether I set near, far or leave it unset makes absolutely no difference. I tested on two different phones — iPhone XS and X — and gave it to two different colleagues to test in case I was doing something wrong.

We tested scenes in which part of the frame was near and the rest far, in both day and night. We expected that setting the range restriction to near causes the near part to be in focus and likewise for the far part. But it never works. Why?

In case it matters, I'm changing the range restriction after the device has been added to the session and the session is running.

I also tried forcing the AF algorithm to run after setting the range restriction by doing device.focusMode = .continuousAutoFocus but it doesn't make a difference. Neither does device.focusMode = .autoFocus

Kartick Vaddadi
  • 4,818
  • 6
  • 39
  • 55

1 Answers1

0

You dont need to do the AF Algorithm forcing. The problem you correctly identified. Basically, any modification you do to the device or captureSession after it is added to the captureSession will not be in effect.

The solution is simple:

Before you add the devices as input to your captureSession (and before running captureSession) execute this code:

try! device.lockForConfiguration()
defer { device.unlockForConfiguration() }
device.autoFocusRangeRestriction = .near

Tell me if this solves the problem! :)