From 1e92f623ded9b662e04eb2924932d9ec5dcf4fd1 Mon Sep 17 00:00:00 2001 From: Ernest Ibarolle <63222761+AdAstraCA@users.noreply.github.com> Date: Wed, 1 Feb 2023 23:04:10 -0800 Subject: [PATCH] Consistent Camera/Adjusted UI I made the camera consistently work and adjusted the UI for after you take a photo so it flows better. --- Shared/ContentView.swift | 79 +++++++++++----------------------------- 1 file changed, 21 insertions(+), 58 deletions(-) diff --git a/Shared/ContentView.swift b/Shared/ContentView.swift index e49d80c..ae966cb 100644 --- a/Shared/ContentView.swift +++ b/Shared/ContentView.swift @@ -3,7 +3,7 @@ // Shared // // Created by Delta on 24/5/22. -// Edited by Astra on 1/27/23. +// Edited by Astra on 2/1/23. // import AVFoundation @@ -50,53 +50,20 @@ struct CameraView: View{ camera.Check() }.frame(width: geometry.size.width, height: geometry.size.height / 1.6, alignment: .center).clipped() HStack{ - - // if taken showing save and again take button... - - if camera.isTaken{ - Spacer() - - Button(action: camera.reTake, label: { - - Image(systemName: "arrow.triangle.2.circlepath.camera") - .foregroundColor(.black) - .padding() - .background(Color.white) - .clipShape(Circle()) - }) - .padding(.trailing,10) + Button(action: camera.takePic, label: { - Spacer() - - Button(action: {if !camera.isSaved{camera.savePic()}}, label: { - Text(camera.isSaved ? "Saved" : "Save") - .foregroundColor(.black) - .fontWeight(.semibold) - .padding(.vertical,10) - .padding(.horizontal,20) - .background(Color.white) - .clipShape(Capsule()) - }) - .padding(.leading) - - Spacer() - } - else{ - - Button(action: camera.takePic, label: { + ZStack{ - ZStack{ - - Circle() - .fill(Color.white) - .frame(width: 65, height: 65) - - Circle() - .stroke(Color.white,lineWidth: 2) - .frame(width: 75, height: 75) - } - }) - } + Circle() + .fill(Color.white) + .frame(width: 65, height: 65) + + Circle() + .stroke(Color.white,lineWidth: 2) + .frame(width: 75, height: 75) + } + }) + } .frame(height: 75) ScrollView(.horizontal, showsIndicators: false) { @@ -175,18 +142,19 @@ class CameraModel: NSObject, ObservableObject, AVCapturePhotoCaptureDelegate{ guard let device = AVCaptureDevice.default(for: AVMediaType.video) else { return } - let input = try AVCaptureDeviceInput(device: device) //checking and adding to session if self.session.canAddInput(input){ + print("input") self.session.addInput(input) } //same for output if self.session.canAddOutput(output){ + print("output") self.session.addOutput(self.output) } @@ -200,17 +168,12 @@ class CameraModel: NSObject, ObservableObject, AVCapturePhotoCaptureDelegate{ //take and retake photos func takePic(){ - + print("success") self.output.capturePhoto(with: AVCapturePhotoSettings(), delegate: self) - DispatchQueue.global(qos: .background).async { - - self.session.stopRunning() - - DispatchQueue.main.async { - withAnimation{self.isTaken.toggle()} - } - } + + savePic() + } func reTake(){ @@ -229,7 +192,6 @@ class CameraModel: NSObject, ObservableObject, AVCapturePhotoCaptureDelegate{ } func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) { - if error != nil{ return } @@ -242,7 +204,7 @@ class CameraModel: NSObject, ObservableObject, AVCapturePhotoCaptureDelegate{ } func savePic(){ - + print("saving") guard let image = UIImage(data: self.picData) else{return} // saving Image... @@ -279,3 +241,4 @@ struct CameraPreview: UIViewRepresentable { } } +