ios – How can i am going to the subsequent display screen after taking an image? SwiftUI


I’ve an issue with the navigation.
I have to take a photograph and after that open a brand new display screen with the button and the taken picture.

Now it opens the identical display screen however with the taken picture.

Please assist

Right here is the primary display screen

Right here is similar display screen, however i would like one other view right here with one other button

My code

ImagePicker

import SwiftUI


class ImagePickerCoordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate {

    @Binding var isShown: Bool
    @Binding var picture: Picture?

    init(isShown: Binding<Bool>, picture: Binding<Picture?>) {
        _isShown = isShown
        _image = picture
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo information: [UIImagePickerController.InfoKey : Any]) {

        let uiImage = information[UIImagePickerController.InfoKey.originalImage] as! UIImage
        picture = Picture(uiImage: uiImage)
        isShown = false
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        isShown = false
    }
}

struct ImagePicker: UIViewControllerRepresentable {

    @Binding var isShown: Bool
    @Binding var picture: Picture?

    func updateUIViewController(_ uiViewController: UIImagePickerController, context: UIViewControllerRepresentableContext<ImagePicker>) {

    }

    func makeCoordinator() -> ImagePickerCoordinator {
        return ImagePickerCoordinator(isShown: $isShown, picture: $picture)
    }

    func makeUIViewController(context: UIViewControllerRepresentableContext<ImagePicker>) -> UIImagePickerController {
        let picker = UIImagePickerController()
        picker.delegate = context.coordinator
        if !UIImagePickerController.isSourceTypeAvailable(.digital camera){
            picker.sourceType = .photoLibrary
        } else {
            picker.sourceType = .digital camera
        }
        return picker
    }

}

ContentView

import SwiftUI

struct ContentView: View {

    @State non-public var showImagePicker: Bool = false
    @State non-public var picture: Picture? = nil

    var physique: some View {
        VStack {

            picture?.resizable()
                .scaledToFit()

            Button("Connect the picture") {
                self.showImagePicker = true
            }.padding()
                .background(Colour.blue)
                .foregroundColor(Colour.white)
                .cornerRadius(10)
        }.sheet(isPresented: self.$showImagePicker) {
            PhotoCaptureView(showImagePicker: self.$showImagePicker, picture: self.$picture)
        }
    }
} 

PhotoCaptureView

import SwiftUI

struct PhotoCaptureView: View {

    @Binding var showImagePicker: Bool
    @Binding var picture: Picture?


    var physique: some View {
        ImagePicker(isShown: $showImagePicker, picture: $picture)
    }
}

#if DEBUG
struct PhotoCaptureView_Previews: PreviewProvider {
    static var previews: some View {
        PhotoCaptureView(showImagePicker: .fixed(false), picture: .fixed(Picture("")))
    }
}
#endif

Thanks

See also  Constructing a world storage for Vapor

Leave a Reply