p-mazhnik

fix(ios): fix initial torch state

@@ -132,17 +132,6 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega @@ -132,17 +132,6 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
132 throw MobileScannerError.noCamera 132 throw MobileScannerError.noCamera
133 } 133 }
134 134
135 - // Enable the torch if parameter is set and torch is available  
136 - if (device.hasTorch && device.isTorchAvailable) {  
137 - do {  
138 - try device.lockForConfiguration()  
139 - device.torchMode = torch  
140 - device.unlockForConfiguration()  
141 - } catch {  
142 - throw MobileScannerError.torchError(error)  
143 - }  
144 - }  
145 -  
146 device.addObserver(self, forKeyPath: #keyPath(AVCaptureDevice.torchMode), options: .new, context: nil) 135 device.addObserver(self, forKeyPath: #keyPath(AVCaptureDevice.torchMode), options: .new, context: nil)
147 captureSession.beginConfiguration() 136 captureSession.beginConfiguration()
148 137
@@ -174,6 +163,13 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega @@ -174,6 +163,13 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
174 } 163 }
175 captureSession.commitConfiguration() 164 captureSession.commitConfiguration()
176 captureSession.startRunning() 165 captureSession.startRunning()
  166 + // Enable the torch if parameter is set and torch is available
  167 + // torch should be set after 'startRunning' is called
  168 + do {
  169 + try toggleTorch(torch)
  170 + } catch {
  171 + print("Failed to set initial torch state.")
  172 + }
177 let dimensions = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription) 173 let dimensions = CMVideoFormatDescriptionGetDimensions(device.activeFormat.formatDescription)
178 174
179 return MobileScannerStartParameters(width: Double(dimensions.height), height: Double(dimensions.width), hasTorch: device.hasTorch, textureId: textureId) 175 return MobileScannerStartParameters(width: Double(dimensions.height), height: Double(dimensions.width), hasTorch: device.hasTorch, textureId: textureId)
@@ -203,6 +199,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega @@ -203,6 +199,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
203 if (device == nil) { 199 if (device == nil) {
204 throw MobileScannerError.torchWhenStopped 200 throw MobileScannerError.torchWhenStopped
205 } 201 }
  202 + if (device.hasTorch && device.isTorchAvailable) {
206 do { 203 do {
207 try device.lockForConfiguration() 204 try device.lockForConfiguration()
208 device.torchMode = torch 205 device.torchMode = torch
@@ -211,6 +208,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega @@ -211,6 +208,7 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
211 throw MobileScannerError.torchError(error) 208 throw MobileScannerError.torchError(error)
212 } 209 }
213 } 210 }
  211 + }
214 212
215 // Observer for torch state 213 // Observer for torch state
216 public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) { 214 public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {