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: Tue Dec 06 2022 11:46:14 GMT-0500 (GMT-05:00)