another.im-ios/AnotherIM/View/Main/Conversation/Attachments/MediaPickerComponents/CameraView.swift

39 lines
1.1 KiB
Swift
Raw Permalink Normal View History

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
}
}