diff --git a/Shared/ContentView.swift b/Shared/ContentView.swift index ae966cb..e49d80c 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 2/1/23. +// Edited by Astra on 1/27/23. // import AVFoundation @@ -50,20 +50,53 @@ struct CameraView: View{ camera.Check() }.frame(width: geometry.size.width, height: geometry.size.height / 1.6, alignment: .center).clipped() HStack{ - Button(action: camera.takePic, label: { + + // if taken showing save and again take button... + + if camera.isTaken{ + Spacer() - ZStack{ - - Circle() - .fill(Color.white) - .frame(width: 65, height: 65) + Button(action: camera.reTake, label: { + + Image(systemName: "arrow.triangle.2.circlepath.camera") + .foregroundColor(.black) + .padding() + .background(Color.white) + .clipShape(Circle()) + }) + .padding(.trailing,10) + + 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: { - Circle() - .stroke(Color.white,lineWidth: 2) - .frame(width: 75, height: 75) - } - }) - + ZStack{ + + 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) { @@ -142,19 +175,18 @@ 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) } @@ -168,12 +200,17 @@ class CameraModel: NSObject, ObservableObject, AVCapturePhotoCaptureDelegate{ //take and retake photos func takePic(){ - print("success") - self.output.capturePhoto(with: AVCapturePhotoSettings(), delegate: self) - - savePic() + self.output.capturePhoto(with: AVCapturePhotoSettings(), delegate: self) + DispatchQueue.global(qos: .background).async { + + self.session.stopRunning() + + DispatchQueue.main.async { + withAnimation{self.isTaken.toggle()} + } + } } func reTake(){ @@ -192,6 +229,7 @@ class CameraModel: NSObject, ObservableObject, AVCapturePhotoCaptureDelegate{ } func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) { + if error != nil{ return } @@ -204,7 +242,7 @@ class CameraModel: NSObject, ObservableObject, AVCapturePhotoCaptureDelegate{ } func savePic(){ - print("saving") + guard let image = UIImage(data: self.picData) else{return} // saving Image... @@ -241,4 +279,3 @@ struct CameraPreview: UIViewRepresentable { } } -