CameraSDKImpl Swift
CameraSDKImpl Swift
// CameraSDK.swift
// CameraSDK
//
// Created by Vlad Cotfas on 17.09.2024.
//
import Foundation
import AVFoundation
import LocalAuthentication
import UIKit
switch cameraAuthorizationStatus {
case .denied, .restricted:
// Access denied or restricted, redirect user to settings
print("Camera needs permission, please enable it from settings")
openSettings()
case .notDetermined, .authorized:
// Check if camera is available
if UIImagePickerController.isSourceTypeAvailable(.camera) {
openCamera()
} else {
// Camera is not available
print("Camera is not available")
delegate?.didOnError("Camera is not available")
}
@unknown default:
// Other errors
print("Other errors on takePhoto")
delegate?.didOnError("Other errors on takePhoto")
}
}
self.notifyDelegateOnMainThreadWithPhotos(sortedPhotoURLs)
} catch {
self.notifyDelegateOnMainThreadWithPhotos(nil)
}
}
}
// overridden methods
public func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true) {
if let image = info[.originalImage] as? UIImage {
// Save image to disk
self.saveImage(image)
} else {
self.delegate?.didTakePhoto(success: false, photoURL: nil)
}
}
}
// overridden methods
public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
self.delegate?.didTakePhoto(success: false, photoURL: nil)
picker.dismiss(animated: true, completion: nil)
}
do {
try imageData.write(to: fileURL)
self.notifyDelegateOnMainThread(success: true, photoURL: fileURL)
} catch {
self.notifyDelegateOnMainThread(success: false, photoURL: nil)
}
}
}