SPB Git

spb/focale Public

Swift 100%
1.4 KB · 46 lines swift
Raw Blame History
1//2//  CameraPreviewView.swift3//  Focale4//5//  Author: Simon-Pierre Boucher6//  Contact: contact@spboucher.ai7//89@preconcurrency import AVFoundation10import SwiftUI1112/// AVCaptureVideoPreviewLayer host. The session runs on its own queue;13/// attaching it to the layer here is safe and cheap.14struct CameraPreviewView: UIViewRepresentable {15    let session: AVCaptureSession16    /// Changing this re-runs updateUIView once the session has its inputs,17    /// so the rotation lands on a connection that actually exists.18    var isSessionConfigured: Bool = false1920    func makeUIView(context: Context) -> PreviewLayerView {21        let view = PreviewLayerView()22        view.previewLayer.session = session23        view.previewLayer.videoGravity = .resizeAspectFill24        applyPortraitRotation(to: view)25        return view26    }2728    func updateUIView(_ uiView: PreviewLayerView, context: Context) {29        applyPortraitRotation(to: uiView)30    }3132    private func applyPortraitRotation(to view: PreviewLayerView) {33        if let connection = view.previewLayer.connection,34           connection.isVideoRotationAngleSupported(90) {35            connection.videoRotationAngle = 9036        }37    }3839    final class PreviewLayerView: UIView {40        override static var layerClass: AnyClass { AVCaptureVideoPreviewLayer.self }41        var previewLayer: AVCaptureVideoPreviewLayer {42            layer as! AVCaptureVideoPreviewLayer43        }44    }45}46