質問編集履歴

1

コードの追加

2020/04/02 02:22

投稿

kjfnfljnf
kjfnfljnf

スコア23

test CHANGED
File without changes
test CHANGED
@@ -14,11 +14,145 @@
14
14
 
15
15
  ViewController1にはカメラのプレビュー(AVCaptureVideoPreviewLayer)があり、ボタンが押されたと同時に、シャッターを切り、ViewController2に移動します。
16
16
 
17
+
18
+
17
19
  しかし、NavigationBarにある、Backボタンを押し、ViewController1に戻ると、カメラのプレビューの部分が画像になってしまい、アプリをシャットダウンしない限り、写真を取り直すことができません。
18
20
 
19
21
 
20
22
 
21
- ViewController2から1へ戻ってきた時にviewDidLoad/ viewWillAppearが呼ばれていないとには気付けたですが、どのように解決すれば良で辿り着けませんでした
23
+ ViewController1のカメラのプレビューを起動し撮影するまでのコードはこのようになっていま
24
+
25
+
26
+
27
+ ```Swift
28
+
29
+ override func viewDidLayoutSubviews() {
30
+
31
+ //Set the frame of preview to make it same as cameraview
32
+
33
+ previewLayer?.frame = cameraView.bounds
34
+
35
+ }
36
+
37
+
38
+
39
+ // Called befor the screen is loaded
40
+
41
+ override func viewWillAppear(_ animated: Bool) {
42
+
43
+ setPreview()
44
+
45
+ }
46
+
47
+
48
+
49
+ func setPreview() {
50
+
51
+ // Initialize session and output
52
+
53
+ session = AVCaptureSession()
54
+
55
+ output = AVCapturePhotoOutput()
56
+
57
+
58
+
59
+ // Resolution setting
60
+
61
+ //session.sessionPreset = AVCaptureSessionPreset1920x1080
62
+
63
+ session.sessionPreset = AVCaptureSession.Preset.hd1920x1080
64
+
65
+
66
+
67
+ let camera = AVCaptureDevice.default(for: AVMediaType.video)
68
+
69
+
70
+
71
+ do {
72
+
73
+ // input camera to device
74
+
75
+ let input = try AVCaptureDeviceInput(device: camera!)
76
+
77
+
78
+
79
+ // Input
80
+
81
+ if (session.canAddInput(input)) {
82
+
83
+ session.addInput(input)
84
+
85
+
86
+
87
+ // Output
88
+
89
+ if (session.canAddOutput(output!)) {
90
+
91
+ session.addOutput(output!)
92
+
93
+
94
+
95
+ // Run camera
96
+
97
+ session.startRunning()
98
+
99
+
100
+
101
+ // Generate Preview
102
+
103
+ previewLayer = AVCaptureVideoPreviewLayer(session: session)
104
+
105
+
106
+
107
+ // Dont't change aspect ratio
108
+
109
+ previewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
110
+
111
+
112
+
113
+ // Add preview
114
+
115
+ self.cameraView.layer.addSublayer(previewLayer!)
116
+
117
+ }
118
+
119
+ }
120
+
121
+ } catch {
122
+
123
+ print(error)
124
+
125
+ }
126
+
127
+ }
128
+
129
+
130
+
131
+ @IBAction func ScanTapped(_ sender: Any) {
132
+
133
+ // Camera setting
134
+
135
+ let settingsForMonitoring = AVCapturePhotoSettings()
136
+
137
+ settingsForMonitoring.flashMode = .off
138
+
139
+ settingsForMonitoring.isHighResolutionPhotoEnabled = false =
140
+
141
+
142
+
143
+ // Shoot
144
+
145
+ output?.capturePhoto(with: settingsForMonitoring, delegate: self)
146
+
147
+ }
148
+
149
+ ```
150
+
151
+
152
+
153
+
154
+
155
+ クラス内の変数を全てnilにすると解決するかと思ったのですが、解決しませんでした。
22
156
 
23
157
 
24
158