gpt4 book ai didi

ios - 无法让自定义相机显示正在捕获的内容的预览

转载 作者:行者123 更新时间:2023-11-28 13:26:55 26 4
gpt4 key购买 nike

我正在尝试开发自定义相机。我的代码如下。我目前无法让应用程序显示正在捕获的内容?我正在使用我的实体 iPhone 来测试该应用。

我正在尝试开发自定义相机。我的代码如下。我目前无法让应用程序显示正在捕获的内容?我正在使用我的实体 iPhone 来测试该应用。

屏幕截图:

enter image description here

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
}

关于ios - 无法让自定义相机显示正在捕获的内容的预览,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58203054/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com