Created
August 16, 2022 06:52
-
-
Save erdemildiz/ed83a1727854c838310cca8af4a5b2cc to your computer and use it in GitHub Desktop.
Camera Session
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import UIKit | |
import AVFoundation | |
enum CameraSession: Error { | |
case inValidInput | |
case inValidOutput | |
} | |
final class CameraSessionViewController: UIViewController { | |
// Camera Setting | |
private let captureSession = AVCaptureSession() | |
private var photoOutput: AVCapturePhotoOutput = { | |
let output = AVCapturePhotoOutput() | |
output.setPreparedPhotoSettingsArray([.init(format: [ | |
AVVideoCodecKey: AVVideoCodecType.jpeg | |
])]) | |
return output | |
}() | |
// Output | |
var takenImage: UIImage? | |
weak var delegate: AVCapturePhotoCaptureDelegate? | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
prepareCamera() | |
} | |
private func prepareCamera() { | |
checkAuthorization() | |
} | |
func takeAPicture() { | |
guard let delegate = delegate else { | |
return | |
} | |
let photoSettings = AVCapturePhotoSettings() | |
photoOutput.capturePhoto(with: photoSettings, delegate: delegate) | |
} | |
} | |
// MARK: Authorization | |
extension CameraSessionViewController { | |
private func checkAuthorization() { | |
let authorizationStatus = AVCaptureDevice.authorizationStatus(for: .video) | |
switch authorizationStatus { | |
case .authorized: | |
setupCaptureSession() | |
case .notDetermined, .denied: | |
requestAuthorization() | |
default: | |
dismiss(animated: true) | |
} | |
} | |
private func requestAuthorization() { | |
AVCaptureDevice.requestAccess(for: .video) { granted in | |
if granted { | |
DispatchQueue.main.async { | |
self.setupCaptureSession() | |
} | |
} | |
} | |
} | |
} | |
// MARK: Setup Camera | |
extension CameraSessionViewController { | |
private func setupCaptureSession() { | |
if let device = AVCaptureDevice.default(for: .video) { | |
do { | |
let input = try AVCaptureDeviceInput(device: device) | |
// Add camera as input | |
guard captureSession.canAddInput(input) else { | |
throw CameraSession.inValidInput | |
} | |
captureSession.addInput(input) | |
// Add output | |
guard captureSession.canAddOutput(photoOutput) else { | |
throw CameraSession.inValidOutput | |
} | |
captureSession.addOutput(photoOutput) | |
// Add camera preview | |
addCameraPreview() | |
// Start camera session | |
captureSession.startRunning() | |
} catch let error { | |
Logger.log(type: .error, message: error.localizedDescription) | |
} | |
} | |
} | |
private func addCameraPreview() { | |
let cameraPreviewLayer = AVCaptureVideoPreviewLayer(session: captureSession) | |
cameraPreviewLayer.frame = view.frame | |
cameraPreviewLayer.videoGravity = .resizeAspectFill | |
view.layer.addSublayer(cameraPreviewLayer) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment