Awalz / SwiftyCam

A Snapchat Inspired iOS Camera Framework written in Swift
BSD 2-Clause "Simplified" License
2.08k stars 327 forks source link

Set zoom level programatically #249

Closed estebanjb1989 closed 2 years ago

estebanjb1989 commented 2 years ago

Hello! I've integrated SwiftyCam to my project and works awesome! But I'm having an issue with the zoom and I'd need to find a way to reset the zoom level to the default when the camera appears.

override func viewDidAppear(_ animated: Bool) {
  super.viewDidAppear(animated)
  captureButton.delegate = self
  if let callback = onCameraReady {
    callback()
  }

  // would be great to call something like this
  self.resetZoomLevel()

  // or like this
  self.setZoomLevel(3)
}

Do you know how can we achieve this?

estebanjb1989 commented 2 years ago

Just copied the source folder to my project and added this method to SwiftyCamViewController:


@objc public func setZoomLevel(zoom: CGFloat) {
        do {
            let captureDevice = AVCaptureDevice.devices().first
            try captureDevice?.lockForConfiguration()

            zoomScale = zoom

            captureDevice?.videoZoomFactor = zoomScale

            DispatchQueue.main.async {
                self.cameraDelegate?.swiftyCam(self, didChangeZoomLevel: self.zoomScale)
            }

            captureDevice?.unlockForConfiguration()

        } catch {
            print("[SwiftyCam]: Error locking configuration")
        }
    }