How to transfer an Image from one screen to another screen in SwiftUI?

120 Views Asked by At

I am creating a feature where a user can take a picture of an item and that image can be displayed on their home screen. I am currently allowing them to take a picture in the AddProductView screen and I want the picture that they took/choose to be shown on the HomeScreenView. I tried to put a @Binding var productImage = UIImage() in the HomeScreenView but that is giving me a

No exact matches in a call to initializer

Here is the HomeScreenView:

struct HomeScreenView: View {
    @Binding var loggedin: Bool
    @State var showSheet: Bool = false
    @Binding var productImage = UIImage()
    var body: some View {
        NavigationView {
            VStack {
                Text("Authorized.... You are in!")
                    .font(.largeTitle)
            }
            .padding()
            .navigationTitle("Home")
            .toolbar {
                ToolbarItemGroup(placement: .navigationBarTrailing){
                    Button () {
                        showSheet.toggle()
                    } label: {
                        Text("Add Item")
                    }
                    .sheet(isPresented: $showSheet, content: {
                        AddProductView()
                    })
                }
            }
        }
        .environment(\.colorScheme, .light)
        .navigationBarBackButtonHidden(true)
    }
}

Here is the AddProductView:

struct AddProductView: View {
    @Environment(\.presentationMode) var presentationMode
    @State var itemImage = false
    @State var openCameraRoll = false
    @State var camera = false
    @State var photoLib = false
    
    @State private var productimage = UIImage()
    @State var productname = ""
    @State var productcategory = ""
    @State var otherproductcategory = ""
    let category = ["Living Room", "Bedroom", "Kitchen", "Garage", "Office", "Electronics", "Clothes"]
    
    var body: some View {
        NavigationView {
                VStack{
                    Form{
                        Section(header: Text("Add An Image")) {
                            if itemImage {
                                Image(uiImage: productimage)
                                    .resizable()
                                    .aspectRatio(contentMode: .fit)
                                    .frame(alignment: .center)
                            } else {
                                Image("ImageButton")
                                    .frame(width: 300, alignment: .center)
                            }
                          
                            HStack{
                                Button(action: {
                                    itemImage = true
                                    openCameraRoll = true
                                    camera = true
                                }, label: {
                                    Text("Camera")
                                        .frame(maxWidth: .infinity, alignment: .center)
                                })
                                .buttonStyle(BorderlessButtonStyle())
                                
                                Divider()
                                
                                Button(action: {
                                    itemImage = true
                                    openCameraRoll = true
                                    camera = false
                                    photoLib = true
                                }, label: {
                                    Text("Photos")
                                        .frame(maxWidth: .infinity, alignment: .center)
                                })
                                .buttonStyle(BorderlessButtonStyle())
                                
                            }
                    
                        }
                        Section(header: Text("Item Information")) {
                            TextField("Item Name", text: $productname)
                            Picker("Category", selection: $productcategory) {
                                ForEach(category, id: \.self){
                                    Text("\($0)")
                                }
                                Text("Other").tag("other")
                            }
                            if productcategory == "other" {
                                TextField("Other Provider", text: $otherproductcategory)
                            }
                            
                        }
                        
                        Button(action: {
                            presentationMode.wrappedValue.dismiss()
                        }, label: {
                            Text("Submit")
                                .frame(maxWidth: .infinity, alignment: .center)
                        })
            
                    }
                    
                }
                .navigationTitle("Add An Item")
                .sheet(isPresented: $openCameraRoll) {
                    if camera {
                        ImagePicker(selectedImage: $productimage, sourceType: .camera)
                    } else {
                        ImagePicker(selectedImage: $productimage, sourceType: .photoLibrary)
                    }
                    
                }
        }
        
    }

Here is where the Image is being set to a variable:

.sheet(isPresented: $openCameraRoll) {
    if camera {
        ImagePicker(selectedImage: $productimage, sourceType: .camera)
    } else {
        ImagePicker(selectedImage: $productimage, sourceType: .photoLibrary)
    }
    
}

I tried to use @Binding var productImage = UIImage() but I get a "No exact matches in a call to initializer"

0

There are 0 best solutions below