ARSession CurrentFrame 缺少 AR 解释模型实体

ARSession CurrentFrame is missing the AR interpretation Model Entities

我有以下 ARView:

import SwiftUI
import UIKit
import RealityKit
import ARKit


struct ARViewContainer: UIViewRepresentable {
    
    @EnvironmentObject var selectedFood: SelectedFood
    @EnvironmentObject var arSession: ARSessionObservable
    
    func makeCoordinator() -> Coordinator {
        Coordinator(self)
    }
  
    func makeUIView(context: Context) -> ARView {
        let arView = ARView(frame: .zero)
        let config = ARWorldTrackingConfiguration()
        config.planeDetection = [.vertical, .horizontal]
        config.environmentTexturing = .automatic
        
        if ARWorldTrackingConfiguration.supportsSceneReconstruction(.mesh) {
            config.sceneReconstruction = .mesh
        }
        arView.session.delegate = context.coordinator
        arView.session.run(config)
        arSession.session = arView.session
        return arView
    }
    
    
    func updateUIView(_ uiView: ARView, context: Context) {
  
        
        if (!selectedFood.food.image.isEmpty) {
            let data = try! Data(contentsOf: URL(string: self.selectedFood.food.image)!)
            let fileURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString)
            try! data.write(to: fileURL)
            do {
                let texture = try TextureResource.load(contentsOf: fileURL)
                var material = SimpleMaterial()
                material.baseColor = MaterialColorParameter.texture(texture)
                material.tintColor = UIColor.white.withAlphaComponent(0.99)
                let entity = ModelEntity(mesh: .generatePlane(width: 0.1, height: 0.1), materials: [material])
                let anchor = AnchorEntity(.plane(.any, classification: .any, minimumBounds: .zero))
                anchor.addChild(entity)
               
                uiView.scene.addAnchor(anchor)
            } catch {
                print(error.localizedDescription)
            }
        }
    }
    
    class Coordinator: NSObject, ARSessionDelegate, ARSCNViewDelegate {
        var arVC: ARViewContainer
        
        init(_ arViewContainer: ARViewContainer) {
            self.arVC = arViewContainer
        }
        
        func session(_ session: ARSession, didUpdate frame: ARFrame) {
        }
        
        func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
        }
    }

}

并且在 HomeView 中我有以下两个变量:

@StateObject var arSession: ARSessionObservable = ARSessionObservable()
@State private var capturedImage: UIImage = UIImage()

以下带动作的按钮:

Button {
    if let capturedFrame = arSession.session.currentFrame {
        let ciimg = CIImage(cvPixelBuffer: capturedFrame.capturedImage)
        if let cgImage = convertCIImageToCGImage(inputImage: ciimg) {
            capturedImage = UIImage(cgImage: cgImage).rotate(radians: .pi / 2)
            self.isShowingMail = true
        }
    }
} label: {
    Image("ShareScreen")
        .resizable()
        .aspectRatio(contentMode:.fit)
        .frame(width: 66, height: 66, alignment: .center)
}

从会话中获取 currentFrame 并打开带有附件的邮件共享模型:

.sheet(isPresented: $isShowingMail) {
                MailComposeViewController(toRecipients: [], mailBody: nil, imageAttachment: capturedImage) {
                    self.isShowingMail = false
                }

邮件分享:

 func makeUIViewController(context: UIViewControllerRepresentableContext<MailComposeViewController>) -> MFMailComposeViewController {
        
        let mail = MFMailComposeViewController()
        mail.mailComposeDelegate = context.coordinator
     
        mail.setToRecipients(self.toRecipients)
        if let body = mailBody {
            mail.setMessageBody(body, isHTML: true)
        }
        if let image = imageAttachment {
            if let imageData = image.pngData() {
                mail.addAttachmentData(imageData, mimeType: "image/png", fileName: "image.png")
            }
        }
        return mail
    }

问题是在预览中存在模型实体,照片如下:

当我按分享时,在邮件预览中框架中缺少模型:

我设法通过将 arView: ARView! 移到 ARViewContainer

之外使其工作
var arView: ARView!

struct ARViewContainer: UIViewRepresentable {

func makeUIView(context: Context) -> ARView {
        arView = ARView(frame: .zero)
        let config = ARWorldTrackingConfiguration()
        config.planeDetection = [.vertical, .horizontal]
        config.environmentTexturing = .automatic
        
        if ARWorldTrackingConfiguration.supportsSceneReconstruction(.mesh) {
            config.sceneReconstruction = .mesh
        }
        arView.session.delegate = context.coordinator
        arView.session.run(config)
        return arView
    }
}

然后在另一个View中调用快照函数给arView

Button {
    arView.snapshot(saveToHDR: false) { image in
        let image = UIImage(data: (image?.pngData())!)
        capturedImage = image!
        self.isShowingMail = true
    }