Code: Select all
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {...}
class CameraModel: NSObject,ObservableObject,AVCapturePhotoCaptureDelegate{
@Published var isTaken = false
@Published var session = AVCaptureSession()
@Published var alert = false
// since were going to read pic data....
@Published var output = AVCapturePhotoOutput()
// preview....
@Published var preview : AVCaptureVideoPreviewLayer!
@Published var image = UIImage()
func Check(){
// first checking camerahas got permission...
switch AVCaptureDevice.authorizationStatus(for: .video) {
case .authorized:
setUp()
return
// Setting Up Session
case .notDetermined:
// retusting for permission....
AVCaptureDevice.requestAccess(for: .video) { (status) in
if status{
self.setUp()
}
}
case .denied:
self.alert.toggle()
return
default:
return
}
}
func setUp(){
// setting up camera...
do{
// setting configs...
self.session.beginConfiguration()
// change for your own...
let device = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: .back)
let input = try AVCaptureDeviceInput(device: device!)
// checking and adding to session...
if self.session.canAddInput(input){
self.session.addInput(input)
}
// same for output....
if self.session.canAddOutput(self.output){
self.session.addOutput(self.output)
}
self.session.commitConfiguration()
}
catch{
print(error.localizedDescription)
}
}
// take and retake functions...
func takePic(){
self.output.capturePhoto(with: AVCapturePhotoSettings(), delegate: self)
DispatchQueue.global(qos: .background).async {
self.session.stopRunning()
DispatchQueue.main.async {
withAnimation{self.isTaken.toggle()}
}
}
}
func reTake(){
DispatchQueue.global(qos: .background).async {
self.session.startRunning()
DispatchQueue.main.async {
withAnimation{self.isTaken.toggle()}
}
}
}
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photo: AVCapturePhoto, error: Error?) {
print("reached")
if error != nil{
return
}
guard let imageData = photo.fileDataRepresentation() else{return}
guard let uiImage = UIImage(data: imageData) else {return}
self.image = uiImage
}
}
< /code>
Wie Sie sich vorstellen können, wird "erreicht" nur manchmal gedruckt, weshalb meine nächste Ansicht manchmal nur manchmal die Benutzeroberfläche enthält. Ich bin mir nicht sicher, was ich von hier aus tun soll.