title: What is wrong with my custom camera view?
body: <p>I followed this video:  <a href="https://www.youtube.com/watch?v=7TqXrMnfJy8&amp;t=45s" rel="nofollow noreferrer">https://www.youtube.com/watch?v=7TqXrMnfJy8&amp;t=45s</a>  to the T. But when I open the camera view all I see is the black screen and white button. I get no error messages when I try I load the camera view. Can someone please assist me with what I'm doing wrong? </p>
  
  <p>My code is below:</p>
  
  <pre><code>import UIKit
  import AVFoundation
  
  class CameraViewController: UIViewController {
  
  var captureSession = AVCaptureSession()
  var backCamera: AVCaptureDevice?
  var currentCamera: AVCaptureDevice?
  
  var photoOutput: AVCapturePhotoOutput?
  
  var cameraPreviewLayer: AVCaptureVideoPreviewLayer?
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      setupCaptureSession()
      setupDevice()
      setupInputOutput()
      setupPreviewLayer()
      startRunningCaptureSession()
  
  }
  
  func setupCaptureSession(){
      captureSession.sessionPreset = AVCaptureSession.Preset.photo
  }
  func setupDevice(){
      let deviceDiscoverySession = AVCaptureDevice.DiscoverySession(deviceTypes: [AVCaptureDevice.DeviceType.builtInWideAngleCamera], mediaType: AVMediaType.video, position: AVCaptureDevice.Position.unspecified)
      let devices = deviceDiscoverySession.devices
  
      for device in devices{
          if device.position == AVCaptureDevice.Position.back {
              backCamera = device
          }
      }
  
      currentCamera = backCamera
  }
  
  func setupInputOutput(){
  
      do {
          let captureDeviceInput = try AVCaptureDeviceInput(device: currentCamera!)
          captureSession.addInput(captureDeviceInput)
          photoOutput?.setPreparedPhotoSettingsArray([AVCapturePhotoSettings(format:[AVVideoCodecKey: AVVideoCodecType.jpeg])], completionHandler: nil)
      } catch {
          print(error)
      }
  
  }
  
  func setupPreviewLayer(){
      cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
      cameraPreviewLayer?.videoGravity = AVLayerVideoGravity.resizeAspectFill
      cameraPreviewLayer?.connection?.videoOrientation = AVCaptureVideoOrientation.portrait
      cameraPreviewLayer?.frame = self.view.frame
      self.view.layer.insertSublayer(cameraPreviewLayer!, at: 1)
  }
  
  func startRunningCaptureSession(){
      captureSession.startRunning()
  }
  
  override func didReceiveMemoryWarning() {
      super.didReceiveMemoryWarning()
      // Dispose of any resources that can be recreated
  }
  }
  </code></pre>