import SwiftUI
import AVFoundation

// MARK: - 相机预览视图
struct CameraPreviewView: UIViewRepresentable {
    let session: AVCaptureSession
    @Binding var previewLayer: AVCaptureVideoPreviewLayer?
    
    func makeUIView(context: Context) -> UIView {
        let view = UIView()
        view.backgroundColor = .black
        
        let previewLayer = AVCaptureVideoPreviewLayer(session: session)
        previewLayer.videoGravity = .resizeAspectFill
        view.layer.addSublayer(previewLayer)
        
        // 设置预览层为绑定变量
        DispatchQueue.main.async {
            self.previewLayer = previewLayer
        }
        
        return view
    }
    
    func updateUIView(_ uiView: UIView, context: Context) {
        guard let previewLayer = uiView.layer.sublayers?.first as? AVCaptureVideoPreviewLayer else { return }
        
        // 确保预览层尺寸正确
        DispatchQueue.main.async {
            previewLayer.frame = uiView.bounds
            self.previewLayer = previewLayer
        }
    }
} 