今天會介紹 生命週期裡要如何宣告
viewDidLoad
在 viewDidLoad 裡呼叫掃描 QRCode 的 function
override func viewDidLoad() {
super.viewDidLoad()
scanQRCode()
}
viewWillAppear
在 viewWillAppear 裡確定掃瞄器有開啟
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
captureSession?.commitConfiguration()
// 判斷 AVCaptureSession 的接收器是否正在執行
if (captureSession?.isRunning == false) {
captureSession?.startRunning()
}
}
viewWillDisappear
在 viewWillDisappear 裡關閉掃瞄器,避免其在背景依舊執行掃描的動作
override func viewWillDisappear(_ animated: Bool)
{
super.viewWillDisappear(animated)
if (captureSession?.isRunning == true) {
captureSession?.stopRunning()
}
}
明天將會繼續介紹 AVCaptureVideoPreviewLayer!拭目以待!
GitHub - AVCaptureVideoPreviewLayerDemo