add missing DispatchQueue.main switch for scale & torch on iOS
Showing
1 changed file
with
21 additions
and
12 deletions
| @@ -215,26 +215,35 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega | @@ -215,26 +215,35 @@ public class MobileScanner: NSObject, AVCaptureVideoDataOutputSampleBufferDelega | ||
| 215 | 215 | ||
| 216 | backgroundQueue.async { | 216 | backgroundQueue.async { |
| 217 | self.captureSession.startRunning() | 217 | self.captureSession.startRunning() |
| 218 | - | ||
| 219 | - // Turn on the flashlight if requested, | ||
| 220 | - // but after the capture session started. | 218 | + |
| 219 | + // After the capture session started, turn on the torch (if requested) | ||
| 220 | + // and reset the zoom scale back to the default. | ||
| 221 | + // Ensure that these adjustments are done on the main DispatchQueue, | ||
| 222 | + // as they interact with the hardware camera. | ||
| 221 | if (torch) { | 223 | if (torch) { |
| 224 | + DispatchQueue.main.async { | ||
| 225 | + do { | ||
| 226 | + try self.toggleTorch(.on) | ||
| 227 | + } catch { | ||
| 228 | + // If the torch does not turn on, | ||
| 229 | + // continue with the capture session anyway. | ||
| 230 | + } | ||
| 231 | + } | ||
| 232 | + } | ||
| 233 | + | ||
| 234 | + DispatchQueue.main.async { | ||
| 222 | do { | 235 | do { |
| 223 | - try self.toggleTorch(.on) | 236 | + try self.resetScale() |
| 224 | } catch { | 237 | } catch { |
| 225 | - // If the torch does not turn on, | 238 | + // If the zoom scale could not be reset, |
| 226 | // continue with the capture session anyway. | 239 | // continue with the capture session anyway. |
| 227 | } | 240 | } |
| 228 | } | 241 | } |
| 229 | 242 | ||
| 230 | - do { | ||
| 231 | - try self.resetScale() | ||
| 232 | - } catch { | ||
| 233 | - // If the zoom scale could not be reset, | ||
| 234 | - // continue with the capture session anyway. | ||
| 235 | - } | ||
| 236 | - | ||
| 237 | if let device = self.device { | 243 | if let device = self.device { |
| 244 | + // When querying the dimensions of the camera, | ||
| 245 | + // stay on the background thread, | ||
| 246 | + // as this does not change the configuration of the hardware camera. | ||
| 238 | let dimensions = CMVideoFormatDescriptionGetDimensions( | 247 | let dimensions = CMVideoFormatDescriptionGetDimensions( |
| 239 | device.activeFormat.formatDescription) | 248 | device.activeFormat.formatDescription) |
| 240 | let hasTorch = device.hasTorch | 249 | let hasTorch = device.hasTorch |
-
Please register or login to post a comment