I'm creating a view to take a photo and send it to a server. I made a button to switch the camera from the back camera to the front camera and vice-versa, but I'm not sure how to implement it. I've done my research but I'm still confused on how to implement it in my project because I'm new to iOS programming. Here are some of my codes:
Some of my AVFoundation
variables
private var session: AVCaptureSession!
private var stillImageOutput: AVCapturePhotoOutput!
private var stillImageSettings: AVCapturePhotoSettings!
private var previewLayer: AVCaptureVideoPreviewLayer!
My setupCamera()
function. I call this function on viewDidAppear()
as well
func setupCamera() {
shutterButton.isUserInteractionEnabled = false
if self.session != nil {
self.session.stopRunning()
}
session = AVCaptureSession()
session.sessionPreset = .photo
guard let setCam: AVCaptureDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back) else {return}
if setCam.isFocusModeSupported(.continuousAutoFocus) && setCam.isFocusPointOfInterestSupported {
do {
try setCam.lockForConfiguration()
let focusPoint = CGPoint(x: 0.5, y: 0.5)
setCam.focusPointOfInterest = focusPoint
setCam.focusMode = .continuousAutoFocus
setCam.isSubjectAreaChangeMonitoringEnabled = true
setCam.unlockForConfiguration()
} catch let error {
print(error.localizedDescription)
}
}
AVCaptureDevice.requestAccess(for: AVMediaType.video) { response in
if response {
// Access Granted
} else {
// Access Denied
let alert = KPMAlertController(title: "Sorry", message: "Mohon maaf, untuk melakukan pengambilan foto, anda harus memberi akses penggunaan kamera", icon: UIImage(named: "ic_info_big"))
let okAct = KPMAlertAction(title: "OK", type: .regular, handler : {
// Open setting
UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!)
self.navigationController?.popViewController(animated: true)
})
alert.addAction(okAct)
self.present(alert, animated: true, completion: nil)
}
}
do {
let input: AVCaptureDeviceInput = try AVCaptureDeviceInput(device: setCam)
if self.session.canAddInput(input) {
self.session.addInput(input)
self.stillImageOutput = AVCapturePhotoOutput()
if self.session.canAddOutput(self.stillImageOutput) {
self.session.addOutput(self.stillImageOutput)
self.previewLayer = AVCaptureVideoPreviewLayer(session: self.session)
self.previewLayer.frame = self.cameraView.bounds
self.previewLayer.videoGravity = .resizeAspectFill
self.previewLayer.connection?.videoOrientation = .portrait
self.cameraView.layer.addSublayer(self.previewLayer)
self.shutterButton.isUserInteractionEnabled = true
self.session.startRunning()
self.backButton.superview?.bringSubviewToFront(self.backButton)
self.shutterButton.superview?.bringSubviewToFront(self.shutterButton)
}
}
ProgressHUD.dismiss()
} catch let error {
print(error.localizedDescription)
ProgressHUD.dismiss()
}
}
And I want to implement the switch function here
@IBAction func switchCameraAction(_ sender: Any) {
// The code to implement switching camera functionalities
}
If you need more code feel free to ask me. Any help would be appreciated. Thank you.