23.8 C
New York
Sunday, October 20, 2024

ios – SwiftUI ARView unable to regulate measurement and alignment


I need to have an ARView however solely partial show on the center of iPhone, so I created some code beneath

that is the container of ARView

struct ARViewContainer : UIViewRepresentable {
    var arViewModel: ARViewModel
    
    func makeUIView(context: Context) -> MyARView {
        arViewModel.startSessionDelegate()
        return arViewModel.arView
    }
    
    func updateUIView(_ uiView: MyARView, context: Context) {}
}

that is the ARViewModel

struct ARModel {
    personal(set) var arView : ARView
    
    init() {
        let rect = UIScreen.fundamental.bounds
        let frameRect = CGRect(x: rect.minX, y: rect.minY, width: rect.width, top: rect.top/4)
        print(frameRect)

        arView = ARView(body: frameRect)

        let config = ARWorldTrackingConfiguration()
        config.planeDetection = [.horizontal]
        arView.session.run(config)
    }
}

class ARViewModel: UIViewController, ObservableObject, ARSessionDelegate {
    @Printed personal var mannequin : ARModel = ARModel()
    @Printed var mapImage: UIImage?
    
    var arView : MyARView {
        mannequin.arView
    }

    func startSessionDelegate() {
        mannequin.arView.session.delegate = self
    }
  
    func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {}

    func session(_ session: ARSession, didUpdate body: ARFrame) {
        if let picture = UIImage(pixelBuffer: body.capturedImage) {
            mapImage = LocSystem.rotateImage(picture)
        }    
    }  
}

that is the ContentView

struct ContentView: View {
    @ObservedObject var arViewModel : ARViewModel = ARViewModel()

    @State var mapImage: UIImage?

    var physique: some View {
        
        ZStack {
            if let picture = arViewModel.mapImage {
                Picture(uiImage: picture)
                    .resizable()
                    .scaledToFit()
                    .body(maxWidth: .infinity, maxHeight: .infinity)
                    .border(Coloration.pink)
                    .background(Coloration.cyan)
            } else {
                Coloration.grey
                    .ignoresSafeArea()
                ProgressView()
            }

            ARViewContainer(arViewModel: arViewModel)
                .scaledToFill()
                .body(maxWidth: .infinity)
                .body(top: UIScreen.fundamental.bounds.top / 4, alignment: .high)
                .border(Coloration.blue)
                .cornerRadius(15)
                .padding(25)
       
                Spacer()
        }.navigationBarBackButtonHidden(true)
    }
}

Ultimately, the consequence seems like this
screenshot

The background is the ARView captured picture, The center body(ARView) is small however the image is aligned heart. I need to align it .high. I can not change its crop measurement, alignment and so. Looks as if ARView is so cussed. Why? Please assist

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles