@@ -128,9 +128,6 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
128128
129129 if self . setupResult == . success {
130130 self . session. startRunning ( )
131-
132- // We need to reapply the configuration after starting the camera
133- self . update ( torchMode: self . torchMode)
134131 }
135132
136133 DispatchQueue . main. async {
@@ -227,6 +224,7 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
227224
228225 do {
229226 try videoDevice. lockForConfiguration ( )
227+ self . reconfigureLockedVideoDevice ( videoDevice)
230228
231229 if videoDevice. isFocusPointOfInterestSupported && videoDevice. isFocusModeSupported ( focusBehavior. avFocusMode) {
232230 videoDevice. focusPointOfInterest = devicePoint
@@ -257,14 +255,12 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
257255 self . torchMode = torchMode
258256 guard let videoDevice = self . videoDeviceInput? . device, videoDevice. torchMode != torchMode. avTorchMode else { return }
259257
260- if videoDevice. isTorchModeSupported ( torchMode. avTorchMode) && videoDevice. hasTorch {
261- do {
262- try videoDevice. lockForConfiguration ( )
263- videoDevice. torchMode = torchMode. avTorchMode
264- videoDevice. unlockForConfiguration ( )
265- } catch {
266- print ( " Error setting torch mode: \( error) " )
267- }
258+ do {
259+ try videoDevice. lockForConfiguration ( )
260+ defer { videoDevice. unlockForConfiguration ( ) }
261+ self . reconfigureLockedVideoDevice ( videoDevice)
262+ } catch {
263+ print ( " Error setting torch mode: \( error) " )
268264 }
269265 }
270266 }
@@ -300,24 +296,23 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
300296
301297 self . removeObservers ( )
302298 self . session. beginConfiguration ( )
303- defer { self . session. commitConfiguration ( ) }
299+ defer {
300+ self . session. commitConfiguration ( )
301+ self . resetZoom ( forDevice: videoDevice)
302+ }
304303
305304 // Remove the existing device input first, since using the front and back camera simultaneously is not supported.
306305 self . session. removeInput ( currentViewDeviceInput)
307306
308307 if self . session. canAddInput ( videoDeviceInput) {
309308 self . session. addInput ( videoDeviceInput)
310- self . resetZoom ( forDevice: videoDevice)
311309 self . videoDeviceInput = videoDeviceInput
312310 } else {
313311 // If it fails, put back current camera
314312 self . session. addInput ( currentViewDeviceInput)
315313 }
316314
317315 self . addObservers ( )
318-
319- // We need to reapply the configuration after reloading the camera
320- self . update ( torchMode: self . torchMode)
321316 }
322317 }
323318
@@ -394,9 +389,6 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
394389
395390 if self . metadataOutput. metadataObjectTypes != newTypes {
396391 self . metadataOutput. metadataObjectTypes = newTypes
397-
398- // Setting metadataObjectTypes reloads the camera, we need to reapply the configuration
399- self . update ( torchMode: self . torchMode)
400392 }
401393 }
402394 }
@@ -425,8 +417,6 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
425417 }
426418
427419 self . metadataOutput. rectOfInterest = visibleRect ?? CGRect ( x: 0 , y: 0 , width: 1 , height: 1 )
428- // We need to reapply the configuration after touching the metadataOutput
429- self . update ( torchMode: self . torchMode)
430420 }
431421 }
432422 }
@@ -506,7 +496,10 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
506496 }
507497
508498 session. beginConfiguration ( )
509- defer { session. commitConfiguration ( ) }
499+ defer {
500+ session. commitConfiguration ( )
501+ self . resetZoom ( forDevice: videoDevice)
502+ }
510503
511504 session. sessionPreset = . photo
512505
@@ -518,9 +511,7 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
518511
519512 if session. canAddInput ( videoDeviceInput) {
520513 session. addInput ( videoDeviceInput)
521-
522514 self . videoDeviceInput = videoDeviceInput
523- self . resetZoom ( forDevice: videoDevice)
524515 } else {
525516 return . sessionConfigurationFailed
526517 }
@@ -573,12 +564,20 @@ class RealCamera: NSObject, CameraProtocol, AVCaptureMetadataOutputObjectsDelega
573564 do {
574565 try videoDevice. lockForConfiguration ( )
575566 defer { videoDevice. unlockForConfiguration ( ) }
567+ reconfigureLockedVideoDevice ( videoDevice)
576568 let defaultZoom = defaultZoomFactor ( for: videoDevice)
577569 videoDevice. videoZoomFactor = zoom * defaultZoom
578570 } catch {
579571 print ( " CKCameraKit: setZoomFor error: \( error) ) " )
580572 }
581573 }
574+
575+ // Torch mode will turn off unless set again when the videoDevice is locked and unlocked
576+ private func reconfigureLockedVideoDevice( _ videoDevice: AVCaptureDevice ) {
577+ if videoDevice. isTorchModeSupported ( torchMode. avTorchMode) && videoDevice. hasTorch {
578+ videoDevice. torchMode = torchMode. avTorchMode
579+ }
580+ }
582581
583582 private func normalizedZoom( for videoDevice: AVCaptureDevice ) -> Double {
584583 let defaultZoom = defaultZoomFactor ( for: videoDevice)
0 commit comments