CameraPreview

main

SwiftUI adaption of AVCaptureVideoPreviewLayer
ribtiago/CameraPreview

CameraPreview

SwiftUI adaption of AVCaptureVideoPreviewLayer.

Gives the ability to use the camera preview in SwiftUI views.

E.g.

import SwiftUI
import AVFoundation
import CameraPreview

struct MyView: View {
    
    private let captureSession = AVCaptureSession()
    
    init() {
        // add an input
        guard let captureDevice = AVCaptureDevice.default(for: .video) else { return }
        guard let captureDeviceInput = try? AVCaptureDeviceInput(device: captureDevice) else { return }
        guard self.captureSession.canAddInput(captureDeviceInput) else { return }
        self.captureSession.addInput(captureDeviceInput)
    }
    
    var body: some View {
        CameraPreview(session: self.captureSession)
            .onAppear {
                self.captureSession.startRunning()
            }
            .onDisappear {
                self.captureSession.stopRunning()
            }
    }
}

Description

  • Swift Tools 5.6.0
View More Packages from this Author

Dependencies

  • None
Last updated: Mon Oct 21 2024 14:23:35 GMT-0900 (Hawaii-Aleutian Daylight Time)