this is my first question here so if it's not explained well please let me know.
So basically I'm trying to access the front camera with this code:
captureSession = AVCaptureSession()
captureSession?.sessionPreset = AVCaptureSessionPreset1920x1080
let cameraDevice = AVCaptureDevice.defaultDevice(withDeviceType: AVCaptureDeviceType.builtInWideAngleCamera , mediaType: AVMediaTypeVideo, position: AVCaptureDevicePosition.front)
print(cameraDevice!)
let error: NSError? = nil
do{
let input = try AVCaptureDeviceInput(device: cameraDevice)
print(captureSession.canAddInput(input))
if error == nil && captureSession.canAddInput(input){
captureSession?.addInput(input)
stillImageOutput = AVCaptureStillImageOutput()
if captureSession.canAddOutput(stillImageOutput){
captureSession.addOutput(stillImageOutput)
previewLayer = AVCaptureVideoPreviewLayer(session: captureSession)
previewLayer?.videoGravity = AVLayerVideoGravityResizeAspect
previewLayer?.connection.videoOrientation = AVCaptureVideoOrientation.portrait
cameraView.layer.addSublayer(previewLayer!)
stillImageOutput?.outputSettings = [AVVideoCodecKey:AVVideoCodecJPEG]
captureSession?.startRunning()
}
}
Using that code print(captureSession.canAddInput(input))
returns false
but when i change the position to the back camera everything works like a charm. Am i missing something?
I'm not sure what device you're using, but on an iPhone 6 / 6plus, that preset resolution is too high for the front camera.
As per the Apple documentation, on those devices the highest still image capture resolution is 1280 x 960. Try using a lower preset and see if it works on the front camera.
This could explain why it works fine on the back camera, because that DOES support the higher resolution.