Reputation: 795
I am trying to develop a custom camera. My code is below. I am currently failing to get the app to display what is being captured? I am using my physical iPhone to test the app.
I am trying to develop a custom camera. My code is below. I am currently failing to get the app to display what is being captured? I am using my physical iPhone to test the app.
Screen Shot:
Xcode 11. target 12.4
My code:
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
}
}
Upvotes: 0
Views: 37
Reputation: 100503
Your problem is here
previewLayer.frame = cameraView.bounds
As cameraView.bounds
isn't yet calculated inside viewDidLoad
, Try
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
previewLayer.frame = cameraView.bounds
}
Upvotes: 1