基本上我想要完成的是让 AVCaptureDevice 的前置摄像头成为 AVCaptureSession 期间应用程序上的第一个也是唯一的选项。
我环顾了 StackOverflow,提供的所有方法和答案都已从 iOS 10、Swift 3 和 Xcode 8 开始弃用。
我知道您应该使用 AVCaptureDeviceDiscoverySession 枚举设备并查看它们以区分正面和背面,但我不确定如何这样做。
有人可以帮忙吗?如果是这样就太棒了!
这是我的代码:
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
previewLayer.frame = singleViewCameraSlot.bounds
self.singleViewCameraSlot.layer.addSublayer(previewLayer)
captureSession.startRunning()
}
lazy var captureSession: AVCaptureSession = {
let capture = AVCaptureSession()
capture.sessionPreset = AVCaptureSessionPreset1920x1080
return capture
}()
lazy var previewLayer: AVCaptureVideoPreviewLayer = {
let preview = AVCaptureVideoPreviewLayer(session: self.captureSession)
preview?.videoGravity = AVLayerVideoGravityResizeAspect
preview?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
preview?.bounds = CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height)
preview?.position = CGPoint(x: self.view.bounds.midX, y: …Run Code Online (Sandbox Code Playgroud)