Skip to content

Instantly share code, notes, and snippets.

@sturdysturge
Created May 9, 2022 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sturdysturge/9635c19fb1d0c0d8ae6939c834fc609b to your computer and use it in GitHub Desktop.
Save sturdysturge/9635c19fb1d0c0d8ae6939c834fc609b to your computer and use it in GitHub Desktop.
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