teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

コードの追加

2020/04/02 02:22

投稿

kjfnfljnf
kjfnfljnf

スコア23

title CHANGED
File without changes
body CHANGED
@@ -6,8 +6,75 @@
6
6
  Navigation Controllerで繋がっている2つのViewControllerがあります。
7
7
 
8
8
  ViewController1にはカメラのプレビュー(AVCaptureVideoPreviewLayer)があり、ボタンが押されたと同時に、シャッターを切り、ViewController2に移動します。
9
+
9
10
  しかし、NavigationBarにある、Backボタンを押し、ViewController1に戻ると、カメラのプレビューの部分が画像になってしまい、アプリをシャットダウンしない限り、写真を取り直すことができません。
10
11
 
11
- ViewController2から1へ戻ってきた時に、viewDidLoad/ viewWillAppearが呼ばれていないことには気付けたですがのように解決すれば良で辿り着けませんでした
12
+ ViewController1カメラのプレビューを起動し撮影するまでコードはこのようになっていま
12
13
 
14
+ ```Swift
15
+ override func viewDidLayoutSubviews() {
16
+ //Set the frame of preview to make it same as cameraview
17
+ previewLayer?.frame = cameraView.bounds
18
+ }
19
+
20
+ // Called befor the screen is loaded
21
+ override func viewWillAppear(_ animated: Bool) {
22
+ setPreview()
23
+ }
24
+
25
+ func setPreview() {
26
+ // Initialize session and output
27
+ session = AVCaptureSession()
28
+ output = AVCapturePhotoOutput()
29
+
30
+ // Resolution setting
31
+ //session.sessionPreset = AVCaptureSessionPreset1920x1080
32
+ session.sessionPreset = AVCaptureSession.Preset.hd1920x1080
33
+
34
+ let camera = AVCaptureDevice.default(for: AVMediaType.video)
35
+
36
+ do {
37
+ // input camera to device
38
+ let input = try AVCaptureDeviceInput(device: camera!)
39
+
40
+ // Input
41
+ if (session.canAddInput(input)) {
42
+ session.addInput(input)
43
+
44
+ // Output
45
+ if (session.canAddOutput(output!)) {
46
+ session.addOutput(output!)
47
+
48
+ // Run camera
49
+ session.startRunning()
50
+
51
+ // Generate Preview
52
+ previewLayer = AVCaptureVideoPreviewLayer(session: session)
53
+
54
+ // Dont't change aspect ratio
55
+ previewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
56
+
57
+ // Add preview
58
+ self.cameraView.layer.addSublayer(previewLayer!)
59
+ }
60
+ }
61
+ } catch {
62
+ print(error)
63
+ }
64
+ }
65
+
66
+ @IBAction func ScanTapped(_ sender: Any) {
67
+ // Camera setting
68
+ let settingsForMonitoring = AVCapturePhotoSettings()
69
+ settingsForMonitoring.flashMode = .off
70
+ settingsForMonitoring.isHighResolutionPhotoEnabled = false =
71
+
72
+ // Shoot
73
+ output?.capturePhoto(with: settingsForMonitoring, delegate: self)
74
+ }
75
+ ```
76
+
77
+
78
+ クラス内の変数を全てnilにすると解決するかと思ったのですが、解決しませんでした。
79
+
13
80
  解決方法ご存知のたかいましたらご教授お願いします。