Skip to content

Instantly share code, notes, and snippets.

View rorodriguez116's full-sized avatar

Rolando Rodríguez rorodriguez116

  • United Kingdom
View GitHub Profile
Rapide
.https
.host("openexhangerates.org")
.path("/api/convert/2000/GBP/EUR")
.authorization(.none)
.params(["app_id":"XYZ"])
.execute(.get, decoding: String.self, customErrorType: MyErrorType.self)
.sink { completion in
if case let .failure(error) = completion {
if let err = error as? MyErrorType {
@rorodriguez116
rorodriguez116 / CameraView.swift
Created October 24, 2020 18:45
CameraView in SwiftUI
struct CameraView: View {
@StateObject var model = CameraViewModel()
@State var currentZoomFactor: CGFloat = 1.0
var captureButton: some View {
Button(action: {
model.capturePhoto()
}, label: {
Circle()
@rorodriguez116
rorodriguez116 / SetZoom.swift
Created October 24, 2020 18:41
Configuring the zoom factor in the video capture device.
public func set(zoom: CGFloat){
let factor = zoom < 1 ? 1 : zoom
let device = self.videoDeviceInput.device
do {
try device.lockForConfiguration()
device.videoZoomFactor = factor
device.unlockForConfiguration()
}
catch {
@rorodriguez116
rorodriguez116 / ChangeCamera.swift
Created October 24, 2020 18:34
Changes active camera between front and rear.
// MARK: Device Configuration
/// - Tag: ChangeCamera
public func changeCamera() {
// MARK: Here disable all camera operation related buttons due to configuration is due upon and must not be interrupted
DispatchQueue.main.async {
self.isCameraButtonDisabled = true
}
//
@rorodriguez116
rorodriguez116 / CameraViewModel.swift
Created October 24, 2020 18:03
CameraViewModel -- Handles CameraServices and acts as middleman to CameraView.
import Combine
import AVFoundation
final class CameraViewModel: ObservableObject {
private let service = CameraService()
@Published var photo: Photo!
@Published var showAlertError = false
@rorodriguez116
rorodriguez116 / CameraPreview.swift
Last active October 23, 2020 21:42
CamperaPreview SwiftUI view.
import AVFoundation
struct CameraPreview: UIViewRepresentable {
// 1.
class VideoPreviewView: UIView {
override class var layerClass: AnyClass {
AVCaptureVideoPreviewLayer.self
}
var videoPreviewLayer: AVCaptureVideoPreviewLayer {
@rorodriguez116
rorodriguez116 / PhotoCapture.swift
Created October 23, 2020 21:10
Photo capture function
// MARK: Capture Photo
/// - Tag: CapturePhoto
public func capturePhoto() {
if self.setupResult != .configurationFailed {
self.isCameraButtonDisabled = true
sessionQueue.async {
if let photoOutputConnection = self.photoOutput.connection(with: .video) {
photoOutputConnection.videoOrientation = .portrait
@rorodriguez116
rorodriguez116 / photoCaptureProcessor.swift
Created October 23, 2020 20:58
PhotoCaptureProcessor implemented AVCapturePhotoCaptureDelegate
import Photos
class PhotoCaptureProcessor: NSObject {
lazy var context = CIContext()
private(set) var requestedPhotoSettings: AVCapturePhotoSettings
private let willCapturePhotoAnimation: () -> Void
@rorodriguez116
rorodriguez116 / Stop_CaptureSession.swift
Created October 23, 2020 20:05
Stops camera video capture session
/// - Tag: Stop capture session
public func stop(completion: (() -> ())? = nil) {
sessionQueue.async {
if self.isSessionRunning {
if self.setupResult == .success {
self.session.stopRunning()
self.isSessionRunning = self.session.isRunning
if !self.session.isRunning {
@rorodriguez116
rorodriguez116 / StartCaptureSession.swift
Created October 23, 2020 00:32
Starts the capture session
/// - Tag: Start capture session
public func start() {
// We use our capture session queue to ensure our UI runs smoothly on the main thread.
sessionQueue.async {
if !self.isSessionRunning && self.isConfigured {
switch self.setupResult {
case .success:
self.session.startRunning()
self.isSessionRunning = self.session.isRunning