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,12 +199,14 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega @@ -203,12 +199,14 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega
203 if (device == nil) { 199 if (device == nil) {
204 throw MobileScannerError.torchWhenStopped 200 throw MobileScannerError.torchWhenStopped
205 } 201 }
206 - do {  
207 - try device.lockForConfiguration()  
208 - device.torchMode = torch  
209 - device.unlockForConfiguration()  
210 - } catch {  
211 - throw MobileScannerError.torchError(error) 202 + if (device.hasTorch && device.isTorchAvailable) {
  203 + do {
  204 + try device.lockForConfiguration()
  205 + device.torchMode = torch
  206 + device.unlockForConfiguration()
  207 + } catch {
  208 + throw MobileScannerError.torchError(error)
  209 + }
212 } 210 }
213 } 211 }
214 212