Caps-iOS/Caps/Camera/ContentViewModel.swift

64 lines
1.5 KiB
Swift
Raw Permalink Normal View History

2022-06-10 21:20:49 +02:00
import CoreImage
import AVFoundation
import UIKit
class ContentViewModel: ObservableObject {
@Published var error: Error?
@Published var frame: CGImage?
@Published var image: UIImage?
2022-06-24 12:05:07 +02:00
private var cameraManager: CameraManager {
.shared
}
private var frameManager: FrameManager {
.shared
}
2022-06-10 21:20:49 +02:00
init() {
setupSubscriptions()
}
2022-06-24 12:05:07 +02:00
2022-06-10 21:20:49 +02:00
func setupSubscriptions() {
frameManager.image = nil
frameManager.current = nil
cameraManager.$error
.receive(on: RunLoop.main)
.map { $0 }
.assign(to: &$error)
frameManager.$current
.receive(on: RunLoop.main)
.compactMap { buffer in
guard let image = CGImage.create(from: buffer) else {
return nil
}
let ciImage = CIImage(cgImage: image)
2022-06-24 12:05:07 +02:00
return FrameManager.shared.context.createCGImage(ciImage, from: ciImage.extent)
2022-06-10 21:20:49 +02:00
}
.assign(to: &$frame)
frameManager.$image
.receive(on: RunLoop.main)
.assign(to: &$image)
}
func endCapture() {
cameraManager.stopVideoCaptureSession()
}
func startCapture() {
cameraManager.startVideoCapture()
}
func captureImage() {
cameraManager.capturePhoto(delegate: frameManager)
}
2022-06-21 19:38:51 +02:00
func continuouslyFocusOnMiddle() {
cameraManager.continuouslyFocusOnMiddle()
}
2022-06-10 21:20:49 +02:00
}