-
Notifications
You must be signed in to change notification settings - Fork 3
/
DemoInputPreviewViewController.swift
130 lines (97 loc) 路 3.28 KB
/
DemoInputPreviewViewController.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//
// DemoInputPreviewViewController.swift
// ComponentCameraDemoUIKit
//
// Created by Muukii on 2021/09/21.
//
import Capturer
import Foundation
import MondrianLayout
import UIKit
@available(iOS 15, *)
final class DemoInputPreviewViewController: UIViewController {
private let captureBody = CaptureBody(
configuration: .init {
$0.sessionPreset = .photo
}
)
var isMirrored = false
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
// Set Up
Task {
do {
let indicator = UIActivityIndicatorView(style: .large)
view.mondrian.buildSubviews {
ZStackBlock {
indicator
}
}
indicator.startAnimating()
}
let input = CameraInput.wideAngleCamera(position: .back)
await captureBody.attach(input: input)
let previewOutput = PreviewOutput()
let photoOutput = PhotoOutput()
await captureBody.attach(output: previewOutput)
await captureBody.attach(output: photoOutput)
let previewView = PixelBufferView()
previewView.attach(output: previewOutput)
await captureBody.start()
let ratio = previewOutput.state.inputInfo!.aspectRatioRespectingVideoOrientation
view.subviews.forEach {
$0.removeFromSuperview()
}
view.mondrian.buildSubviews {
LayoutContainer(attachedSafeAreaEdges: .all) {
VStackBlock(alignment: .fill) {
previewView
.viewBlock
.aspectRatio(ratio)
HStackBlock(spacing: 4) {
UIButton.make(title: "Use back") { [unowned self] in
Task { [weak self] in
let input = CameraInput.wideAngleCamera(position: .back)
await captureBody.attach(input: input)
previewOutput.setIsMirroringEnabled(false)
self?.isMirrored = false
}
}
UIButton.make(title: "Use front") { [unowned self] in
Task { [weak self] in
let input = CameraInput.wideAngleCamera(position: .front)
await captureBody.attach(input: input)
previewOutput.setIsMirroringEnabled(true)
self?.isMirrored = true
}
}
UIButton.make(title: "Remove input") { [unowned self] in
Task {
await captureBody.removeCurrentInput()
}
}
UIButton.make(title: "Capture") { [unowned photoOutput, unowned self] in
Task { [weak self] in
do {
let settings = AVCapturePhotoSettings()
settings.isHighResolutionPhotoEnabled = true
let result = try await photoOutput.capture(with: settings)
guard let self = self else { return }
let image = result.makeImage(isMirrored: self.isMirrored)
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
print(image)
} catch {
print(error)
}
}
}
StackingSpacer(minLength: 0)
}
}
}
}
}
// Layout
}
}