[ad_1]
I am making digital camera app and every part works anticipate saving photograph to photograph library.
I added every part in data.plist, for instance privateness for utilization library and so on.
The error is’
Thread 1: Deadly error: Unexpectedly discovered nil whereas unwrapping an Non-obligatory worth – error happens on line 115 in cameraModel actual line:
let picture = UIImage(knowledge: self.picData)!
I run app on actual machine.
That is my digital camera mannequin the place I saved savePic() perform
import Basis
import AVFoundation
import SwiftUI
class CameraModel:NSObject, ObservableObject, AVCapturePhotoCaptureDelegate {
@Revealed var isTaken = false
@Revealed var session = AVCaptureSession()
@Revealed var alert = false
@Revealed var output = AVCapturePhotoOutput()
@Revealed var preview = AVCaptureVideoPreviewLayer()
@Revealed var isSaved = false
@Revealed var picData = Knowledge(depend: 0)
func examine(){
change AVCaptureDevice.authorizationStatus(for: .video) {
case .notDetermined:
AVCaptureDevice.requestAccess(for: .video) { (standing) in
if standing {
self.setUp()
}
}
case .denied:
self.alert.toggle()
return
case .approved:
setUp()
return
case .restricted:
return
@unknown default:
return
}
}
func setUp(){
//organising digital camera
do {
let machine = AVCaptureDevice.default(for:.video)
if machine != nil {
let enter = strive AVCaptureDeviceInput(machine: machine!)
if self.session.canAddInput(enter){
self.session.addInput(enter)
}
} else {
return
}
if self.session.canAddOutput(output){
self.session.addOutput(output)
}
} catch {
print(error.localizedDescription)
}
}
func takePic(){
DispatchQueue.international(qos:.background).async {
self.output.capturePhoto(with: AVCapturePhotoSettings(), delegate: self)
self.session.stopRunning()
DispatchQueue.fundamental.async {
withAnimation{self.isTaken.toggle()}
print("Photograph taken")
}
}
}
func reTake(){
DispatchQueue.international(qos:.background).async {
self.session.startRunning()
DispatchQueue.fundamental.async {
withAnimation{self.isTaken.toggle()}
self.isSaved = false
}
}
}
func photoOutput(_ output: AVCapturePhotoOutput, didFinishProcessingPhoto photograph: AVCapturePhoto, error: Error?) {
if error != nil {
return
}
guard let imageData = photograph.fileDataRepresentation() else { return }
self.picData = imageData
print("pic taken")
}
func savePic(){
let picture = UIImage(knowledge: self.picData)!
UIImageWriteToSavedPhotosAlbum(picture, nil, nil, nil)
self.isSaved = true
}
}
And that is cameraView the place I name savePic()
struct CameraView: View {
@StateObject var digital camera = CameraModel()
var physique: some View {
ZStack{
CameraPreview(digital camera: digital camera)
.ignoresSafeArea(.all)
VStack{
if digital camera.isTaken {
HStack {
Spacer()
Button{
digital camera.reTake()
} label: {
Picture(systemName: "arrow.triangle.2.circlepath.digital camera")
.foregroundColor(.black)
.padding()
.background(Shade.white)
.clipShape(Circle())
}.padding(.trailing,10)
}
}
Spacer()
HStack(){
if digital camera.isTaken{
Button{
if !digital camera.isSaved {
digital camera.savePic()
}
} label: {
Textual content(digital camera.isSaved ? "Saved" : "Save")
.foregroundColor(.black)
.fontWeight(.semibold)
.padding(.vertical,10)
.padding(.horizontal,20)
.background(Shade.white)
.clipShape(Capsule())
}.padding(.main)
Spacer()
} else {
Button{
digital camera.takePic()
} label: {
ZStack{
Circle().fill(Shade.white).body(width: 65, peak: 65)
Circle().stroke(Shade.white,lineWidth: 2)
.body(width: 75, peak: 75)
}
}
}
}
}.onAppear{
digital camera.examine()
}
}
}
}
struct CameraView_Previews: PreviewProvider {
static var previews: some View {
CameraView()
}
}
[ad_2]
