1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//
//  MetalVideoView.swift
//  LiveProject
//  视频绘制
//  Created by 倪路朋 on 6/26/25.
//
import SwiftUI
import MetalKit
 
struct VideoRendererView: UIViewRepresentable {
    @Binding var pixelBuffer: CVPixelBuffer?
    @Binding var rotate:Int?;
 
    //用 Coordinator 缓存实例
    func makeCoordinator() -> Coordinator {
        return Coordinator()
    }
 
    func makeUIView(context: Context) -> MTKView {
        return context.coordinator.mtkView
    }
 
    func updateUIView(_ uiView: MTKView, context: Context) {
        if let buffer = pixelBuffer {
            //print("updateUIView")
            context.coordinator.renderer.display(pixelBuffer: buffer)
        }
        if let angle = rotate{
            //print("updateUIView rotate \(angle)")
            context.coordinator.renderer.updateRotate(angle: angle)
        }
    }
    
 
    class Coordinator {
        let mtkView: MTKView
        let renderer: MetalRenderer
 
        init() {
            print("📦 MetalRendererWrapper 初始化了")
            mtkView = MTKView()
            renderer = MetalRenderer(mtkView: mtkView)
        }
    }
}