无法让自定义相机显示正在捕获的内容的预览
Failing to get the Custom Camera displaying a preview of what is being captured
我正在尝试开发自定义相机。我的代码如下。我目前无法让应用程序显示正在捕获的内容?我正在使用我的身体 iPhone 来测试应用程序。
我正在尝试开发自定义相机。我的代码如下。我目前无法让应用程序显示正在捕获的内容?我正在使用我的身体 iPhone 来测试应用程序。
屏幕截图:
Xcode 11.
目标 12.4
我的代码:
class CameraViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
var margin: UILayoutGuide!
var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!
let topSection: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = UIColor.yellow
return view
}()
let cameraView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
//view.backgroundColor = UIColor.black
return view
}()
let bottomSection: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = UIColor.green
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
setup()
}
private func setup(){
view.backgroundColor = UIColor.white
margin = view.layoutMarginsGuide
captureSession = AVCaptureSession.init()
view.addSubview(topSection)
view.addSubview(bottomSection)
view.addSubview(cameraView)
setupSectionLayouts()
setupCameraCapture()
}
private func setupCameraCapture(){
captureSession.beginConfiguration()
// Configure input
let videoDevice = AVCaptureDevice.default(for: .video)
guard
let videoDeviceInput = try? AVCaptureDeviceInput.init(device: videoDevice!) as AVCaptureInput,
captureSession.canAddInput(videoDeviceInput)
else{
print("unable to add videoDeviceInput")
return
}
captureSession.addInput(videoDeviceInput)
// configure output
let videoOutput = AVCaptureVideoDataOutput.init()
guard captureSession.canAddOutput(videoOutput) else {
print("unable add videoOutput")
return
}
videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue.init(label: "videoQueue"))
captureSession.addOutput(videoOutput)
captureSession.commitConfiguration()
captureSession.startRunning()
guard let session = captureSession else {
print("no captureSession")
return
}
previewLayer = AVCaptureVideoPreviewLayer.init(session: session)
previewLayer.frame = cameraView.bounds
cameraView.layer.insertSublayer(previewLayer, at: 0)
}
}
// Layout
extension CameraViewController{
private func setupSectionLayouts(){
topSection.heightAnchor.constraint(equalTo: margin.heightAnchor, multiplier: 0.1).isActive = true
topSection.topAnchor.constraint(equalTo: margin.topAnchor).isActive = true
topSection.leadingAnchor.constraint(equalTo: margin.leadingAnchor).isActive = true
topSection.trailingAnchor.constraint(equalTo: margin.trailingAnchor).isActive = true
bottomSection.heightAnchor.constraint(equalTo: margin.heightAnchor, multiplier: 0.15).isActive = true
bottomSection.bottomAnchor.constraint(equalTo: margin.bottomAnchor).isActive = true
bottomSection.leadingAnchor.constraint(equalTo: margin.leadingAnchor).isActive = true
bottomSection.trailingAnchor.constraint(equalTo: margin.trailingAnchor).isActive = true
cameraView.topAnchor.constraint(equalTo: topSection.bottomAnchor).isActive = true
cameraView.bottomAnchor.constraint(equalTo: bottomSection.topAnchor).isActive = true
cameraView.leadingAnchor.constraint(equalTo: margin.leadingAnchor).isActive = true
cameraView.trailingAnchor.constraint(equalTo: margin.trailingAnchor).isActive = true
}
}
你的问题就在这里
previewLayer.frame = cameraView.bounds
由于 cameraView.bounds
尚未在 viewDidLoad
中计算,尝试
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
previewLayer.frame = cameraView.bounds
}
我正在尝试开发自定义相机。我的代码如下。我目前无法让应用程序显示正在捕获的内容?我正在使用我的身体 iPhone 来测试应用程序。
我正在尝试开发自定义相机。我的代码如下。我目前无法让应用程序显示正在捕获的内容?我正在使用我的身体 iPhone 来测试应用程序。
屏幕截图:
Xcode 11. 目标 12.4
我的代码:
class CameraViewController: UIViewController, AVCaptureVideoDataOutputSampleBufferDelegate {
var margin: UILayoutGuide!
var captureSession: AVCaptureSession!
var previewLayer: AVCaptureVideoPreviewLayer!
let topSection: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = UIColor.yellow
return view
}()
let cameraView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
//view.backgroundColor = UIColor.black
return view
}()
let bottomSection: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = UIColor.green
return view
}()
override func viewDidLoad() {
super.viewDidLoad()
setup()
}
private func setup(){
view.backgroundColor = UIColor.white
margin = view.layoutMarginsGuide
captureSession = AVCaptureSession.init()
view.addSubview(topSection)
view.addSubview(bottomSection)
view.addSubview(cameraView)
setupSectionLayouts()
setupCameraCapture()
}
private func setupCameraCapture(){
captureSession.beginConfiguration()
// Configure input
let videoDevice = AVCaptureDevice.default(for: .video)
guard
let videoDeviceInput = try? AVCaptureDeviceInput.init(device: videoDevice!) as AVCaptureInput,
captureSession.canAddInput(videoDeviceInput)
else{
print("unable to add videoDeviceInput")
return
}
captureSession.addInput(videoDeviceInput)
// configure output
let videoOutput = AVCaptureVideoDataOutput.init()
guard captureSession.canAddOutput(videoOutput) else {
print("unable add videoOutput")
return
}
videoOutput.setSampleBufferDelegate(self, queue: DispatchQueue.init(label: "videoQueue"))
captureSession.addOutput(videoOutput)
captureSession.commitConfiguration()
captureSession.startRunning()
guard let session = captureSession else {
print("no captureSession")
return
}
previewLayer = AVCaptureVideoPreviewLayer.init(session: session)
previewLayer.frame = cameraView.bounds
cameraView.layer.insertSublayer(previewLayer, at: 0)
}
}
// Layout
extension CameraViewController{
private func setupSectionLayouts(){
topSection.heightAnchor.constraint(equalTo: margin.heightAnchor, multiplier: 0.1).isActive = true
topSection.topAnchor.constraint(equalTo: margin.topAnchor).isActive = true
topSection.leadingAnchor.constraint(equalTo: margin.leadingAnchor).isActive = true
topSection.trailingAnchor.constraint(equalTo: margin.trailingAnchor).isActive = true
bottomSection.heightAnchor.constraint(equalTo: margin.heightAnchor, multiplier: 0.15).isActive = true
bottomSection.bottomAnchor.constraint(equalTo: margin.bottomAnchor).isActive = true
bottomSection.leadingAnchor.constraint(equalTo: margin.leadingAnchor).isActive = true
bottomSection.trailingAnchor.constraint(equalTo: margin.trailingAnchor).isActive = true
cameraView.topAnchor.constraint(equalTo: topSection.bottomAnchor).isActive = true
cameraView.bottomAnchor.constraint(equalTo: bottomSection.topAnchor).isActive = true
cameraView.leadingAnchor.constraint(equalTo: margin.leadingAnchor).isActive = true
cameraView.trailingAnchor.constraint(equalTo: margin.trailingAnchor).isActive = true
}
}
你的问题就在这里
previewLayer.frame = cameraView.bounds
由于 cameraView.bounds
尚未在 viewDidLoad
中计算,尝试
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
previewLayer.frame = cameraView.bounds
}