avfoundationswift5ios-cameraxcode11.4

Swift Camera Zooming


I’m trying to zoom in if a button gets pressed. Apple writes in its Documentation that I have to call lockForConfiguration() and unlockForConfiguration() when im changing the videoZoomFactor’s value. But I’m unsure if this is the right way to implement.

@IBAction func zoomBtnPressed(_ sender: UIButton) {

        do {
            try captureDevice.lockForConfiguration()
        } catch {
            print(error)
        }

        captureDevice.videoZoomFactor = 10

        do {
            try captureDevice.unlockForConfiguration()
        } catch {
            print(error)
        }

    }

For the unlockForConfiguration() I get two warnings.

⚠️ No calls to throwing functions occur within 'try' expression

⚠️ 'catch' block is unreachable because no errors are thrown in 'do' block


Solution

  • Welcome!

    unlockForConfiguration is not a throwing function. You don't need to do { try ... } catch { ... } around it.