James Stacy
James Stacy

Reputation: 33

Video Preview not showing until the app is pushed into the background

I have an app written in SwiftUI that shows a video preview. When I go to the view that should show the preview it is not there. If I start to pull the screen up to put the app in the background the video shows up. I can also just pull it up to put it in back ground and the video show up when I brig it back. the preview is a UIViewRepresentable.

struct CameraPreview: UIViewRepresentable {
  @Binding var session: AVCaptureSession

  func makeUIView(context: Context) -> UIView {
    let view = UIView()
    let previewLayer = AVCaptureVideoPreviewLayer(session: session)
    previewLayer.videoGravity = .resizeAspectFill
    view.layer.addSublayer(previewLayer)
    return view
  }

  func updateUIView(_ uiView: UIView, context: Context) {
    if let layer = uiView.layer.sublayers?.first as? AVCaptureVideoPreviewLayer {
      layer.session = session
      layer.frame = uiView.bounds
    }
  }
}

Upvotes: 0

Views: 37

Answers (0)

Related Questions