-
-
Save sturdysturge/9635c19fb1d0c0d8ae6939c834fc609b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import AVFoundation.AVCaptureSession | |
struct ContentView: View { | |
@ObservedObject var data = DataModel.shared | |
var body: some View { | |
ZStack { | |
CameraFeedView(view: data.previewView, | |
session: data.session) | |
.frame(maxWidth: .infinity, maxHeight: .infinity) | |
DebugOverlayView(text: data.debugString) | |
} | |
} | |
} | |
extension ContentView { | |
private struct DebugOverlayView: View { | |
let text: String | |
var body: some View { | |
Group { | |
ZStack { | |
Rectangle() | |
.foregroundColor(.white) | |
Text(text) | |
.foregroundColor(.black) | |
.frame(maxWidth: .infinity, | |
maxHeight: .infinity, | |
alignment: .topLeading) | |
.font(.title) | |
.padding(15) | |
} | |
.frame(height: 200) | |
.cornerRadius(20) | |
} | |
.frame(maxWidth: .infinity, | |
maxHeight: .infinity, | |
alignment: .bottom) | |
} | |
} | |
private struct CameraFeedView: UIViewRepresentable { | |
let view: UIView | |
let session: AVCaptureSession | |
func makeUIView(context: Context) -> UIView { | |
return view | |
} | |
func updateUIView(_ uiView: UIView, context: Context) { | |
if view.bounds != .zero { | |
DispatchQueue.main.async { | |
AVCaptureVideoPreviewLayer | |
.createAsSublayer(of: view, session: session) | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment