Skip to content

Commit

Permalink
fix: update preview layer to be optional and check for nil values (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
phantumcode authored Apr 9, 2024
1 parent ca1f967 commit bb5cc4a
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions Sources/FaceLiveness/Views/Liveness/LivenessViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Amplify

final class _LivenessViewController: UIViewController {
let viewModel: FaceLivenessDetectionViewModel
var previewLayer: CALayer!
var previewLayer: CALayer?

let faceShapeLayer = CAShapeLayer()
var ovalExists = false
Expand All @@ -39,8 +39,9 @@ final class _LivenessViewController: UIViewController {
}

deinit {
self.previewLayer.removeFromSuperlayer()
(self.previewLayer as? AVCaptureVideoPreviewLayer)?.session = nil
guard let previewLayer = self.previewLayer else { return }
previewLayer.removeFromSuperlayer()
(previewLayer as? AVCaptureVideoPreviewLayer)?.session = nil
self.previewLayer = nil
}

Expand Down Expand Up @@ -84,7 +85,9 @@ final class _LivenessViewController: UIViewController {

avLayer.position = view.center
self.previewLayer = avLayer
viewModel.cameraViewRect = previewLayer.frame
if let previewLayer = self.previewLayer {
viewModel.cameraViewRect = previewLayer.frame
}

DispatchQueue.main.async {
self.view.layer.insertSublayer(avLayer, at: 0)
Expand Down Expand Up @@ -112,11 +115,12 @@ final class _LivenessViewController: UIViewController {
extension _LivenessViewController: FaceLivenessViewControllerPresenter {
func displaySingleFrame(uiImage: UIImage) {
DispatchQueue.main.async {
guard let previewLayer = self.previewLayer else { return }
let imageView = UIImageView(image: uiImage)
imageView.frame = self.previewLayer.frame
imageView.frame = previewLayer.frame
self.view.addSubview(imageView)
self.previewLayer.removeFromSuperlayer()
(self.previewLayer as? AVCaptureVideoPreviewLayer)?.session = nil
(previewLayer as? AVCaptureVideoPreviewLayer)?.session = nil
previewLayer.removeFromSuperlayer()
self.viewModel.stopRecording()
}
}
Expand Down Expand Up @@ -147,14 +151,15 @@ extension _LivenessViewController: FaceLivenessViewControllerPresenter {

func drawOvalInCanvas(_ ovalRect: CGRect) {
DispatchQueue.main.async {
guard let previewLayer = self.previewLayer else { return }
self.faceGuideRect = ovalRect

let ovalView = OvalView(
frame: self.previewLayer.frame,
frame: previewLayer.frame,
ovalFrame: ovalRect
)
self.ovalView = ovalView
ovalView.center = self.previewLayer.position
ovalView.center = previewLayer.position
self.view.insertSubview(
ovalView,
belowSubview: self.freshnessView
Expand Down

0 comments on commit bb5cc4a

Please sign in to comment.