Im trying to add a camera component to my app but I am getting the error: "Publishing changes from within view updates is not allowed, this will cause undefined behavior." on the line "view.layer.addSublayer(camera.preview)" in this section of my code:
struct CameraPreview: UIViewRepresentable {
@ObservedObject var camera : CameraModel
func makeUIView(context: Context) -> UIView {
let view = UIView(frame: UIScreen.main.bounds)
camera.preview = AVCaptureVideoPreviewLayer(session: camera.session)
camera.preview.frame = view.frame
camera.preview.videoGravity = .resizeAspectFill
view.layer.addSublayer(camera.preview)
DispatchQueue.global(qos: .background).async {
camera.session.startRunning()
}
return view
}
}
Does anyone know how I can fix this?