1

I am trying to capture an Image and set it to the UIImageView, hence to create the camera I have the following code:

class HomeController: BaseController, UIImagePickerControllerDelegate {

var detector: AFDXDetector?
var captureSession : AVCaptureSession?
var stillImageOutput : AVCapturePhotoOutput?
var previewLayer : AVCaptureVideoPreviewLayer?
var camera : AVCaptureDevice!

@IBOutlet weak var cameraBtn: UIButton!
@IBOutlet weak var cameraView: UIView!
@IBOutlet weak var cameraImageView: UIImageView!

    override func viewDidLoad() {
      super.viewDidLoad()
      startCamera()
    }

    func startCamera() {

    do {

        captureSession = AVCaptureSession()
        camera = AVCaptureDevice.defaultDevice(withDeviceType: .builtInWideAngleCamera, mediaType: AVMediaTypeVideo, position: .front)
        captureSession?.sessionPreset = AVCaptureSessionPreset1280x720

        let input = try AVCaptureDeviceInput(device: camera)

        if (captureSession?.canAddInput(input))!{
            captureSession?.addInput(input)
            stillImageOutput = AVCapturePhotoOutput()

            if (captureSession?.canAddOutput(stillImageOutput))!{
                print("output added")
                captureSession?.canAddOutput(stillImageOutput)
                previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
                previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
                previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
                cameraView.layer.addSublayer(previewLayer!)
                captureSession?.startRunning()
            }
        }

    } catch {

    }
}


@IBAction func cameraBtnPressed(_ sender: Any) {
   if (stillImageOutput?.connection(withMediaType: AVMediaTypeVideo)) != nil
    {
        print("video connection detected")
    }
}
}

For some reason, the print statement "video connection detected" doesn't get called although the camera is working

Does anyone else know why?

KTOV
  • 559
  • 3
  • 14
  • 39

1 Answers1

1

Within the if statment of captureSession?.canAddOutput(stillImageOutput) change captureSession?.canAddOutput(stillImageOutput) to .addOutput

Tunds
  • 1,804
  • 2
  • 15
  • 30