Skip to content

Commit

Permalink
Async start/stop capturing
Browse files Browse the repository at this point in the history
  • Loading branch information
retendo committed Jun 27, 2020
1 parent 54b5e3f commit bc51dba
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions Sources/BarcodeHeroUI/Controllers/BHCameraScanController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@

private let session = AVCaptureSession()

private let sessionQueue = DispatchQueue(label: "barcodehero.capturesession")

// private var dismissOnScan: Bool = false
private var hasLoaded: Bool = false
private var previewLayer: AVCaptureVideoPreviewLayer?
Expand Down Expand Up @@ -107,7 +109,7 @@

self.focusAreaView.clear()

self.session.startRunning()
startCapturing()
}

override open func viewWillAppear(_ animated: Bool) {
Expand All @@ -124,7 +126,7 @@

edgesForExtendedLayout = UIRectEdge.all

self.session.startRunning()
startCapturing()

self.focusAreaView.clear()
}
Expand Down Expand Up @@ -169,7 +171,7 @@
override open func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)

self.session.stopRunning()
stopCapturing()

// navigationController?.navigationBar.barTintColor = startingBarTintColor
// navigationController?.navigationBar.tintColor = startingTintColor
Expand All @@ -179,11 +181,19 @@
// MARK: Methods - Utilities

public func stopCapturing() {
self.session.stopRunning()
sessionQueue.async {
if self.session.isRunning {
self.session.stopRunning()
}
}
}

public func startCapturing() {
self.session.startRunning()
sessionQueue.async {
if !self.session.isRunning {
self.session.startRunning()
}
}
}
}

Expand Down

0 comments on commit bc51dba

Please sign in to comment.