fmodf
b3b3b3aef7
Reviewed-on: narayana/conversations-classic-ios#1 Co-authored-by: fmodf <fmodf.ios@gmail.com> Co-committed-by: fmodf <fmodf.ios@gmail.com>
39 lines
1.1 KiB
Swift
39 lines
1.1 KiB
Swift
import AVFoundation
|
|
import SwiftUI
|
|
import UIKit
|
|
|
|
class CameraUIView: UIView {
|
|
var previewLayer: AVCaptureVideoPreviewLayer?
|
|
|
|
override func layoutSubviews() {
|
|
super.layoutSubviews()
|
|
previewLayer?.frame = bounds
|
|
}
|
|
}
|
|
|
|
struct CameraView: UIViewRepresentable {
|
|
func makeUIView(context _: Context) -> CameraUIView {
|
|
let view = CameraUIView()
|
|
|
|
let captureSession = AVCaptureSession()
|
|
guard let captureDevice = AVCaptureDevice.default(for: .video) else { return view }
|
|
guard let input = try? AVCaptureDeviceInput(device: captureDevice) else { return view }
|
|
captureSession.addInput(input)
|
|
|
|
let previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
|
|
previewLayer.videoGravity = .resizeAspectFill
|
|
view.layer.addSublayer(previewLayer)
|
|
view.previewLayer = previewLayer
|
|
|
|
DispatchQueue.global(qos: .background).async {
|
|
captureSession.startRunning()
|
|
}
|
|
|
|
return view
|
|
}
|
|
|
|
func updateUIView(_ uiView: CameraUIView, context _: Context) {
|
|
uiView.previewLayer?.frame = uiView.bounds
|
|
}
|
|
}
|