I am trying to integrate an ARKit view which processes frames with machine learning and shows the results on the screen. I have gotten the ARKit view to work with UIViewRepresentable and everything works until a state changes. How do I make the AR view static and not update when a state changes. I only want to update the label that shows the result.
This is the error that I receive when the state changes: [CAMetalLayer nextDrawable] returning nil because allocation failed.
This presumably happens because the arView is being constantly reloaded as it processes the frames? Not too sure though.
This is the code for the view:
struct ARControlView: View {
@EnvironmentObject var resultHandler: ResultHandler
var body: some View {
let arView = ARViewContainer() // This is the UIViewRepresentable containing the ARKit view.
return ZStack {
arView
VStack {
Text(self.resultHandler.gesture.rawValue)
}
.onAppear {
arView.restartARSession()
}
.onDisappear {
arView.pauseArSession()
}
}
}
}
This is for the ARViewContainer:
struct ARViewContainer: UIViewRepresentable {
var arView = ARView(frame: .zero)
@EnvironmentObject var resultHandler: ResultHandler
func makeUIView(context: Context) -> ARView {
arView.session.delegate = context.coordinator
arView.session.run(AROrientationTrackingConfiguration())
return arView
}
func pauseArSession() {
arView.session.pause()
}
func restartARSession() {
arView.session.run(AROrientationTrackingConfiguration())
}
func updateUIView(_ uiView: ARView, context: Context) {}
class Coordinator: NSObject, ARSessionDelegate {
// Process frames here...
}
func makeCoordinator() -> ARViewContainer.Coordinator {
Coordinator(self)
}
}