Home > Mobile >  How to pass UIImage between to 2 Views in SwifUI
How to pass UIImage between to 2 Views in SwifUI

Time:08-17

I want to separate the views, and I created a struct view to get only the image to make the code more cleaner, please I want to ask how can I pass the selected image to the next view, to make the code more cleaner and Can use this struct view everywhere in the project. I put in second view PickAPhoto() but I'm not sure how to get the data from it.

Many Thanks,

struct PickAPhoto: View {
    @State var imgSelected: UIImage = UIImage(named: "addCameraImg")!
    @State var showAddPhotoSheet = false
    // phppicker begin
        @State private var showPhotoSheet = false
    // phppicker end
    @State private var sourceType: UIImagePickerController.SourceType = .camera
    @State var image: Image? = nil
    
    var body: some View {
        Image(uiImage: imgSelected)
            .resizable()
            .cornerRadius(4)
          //  .frame(width: 200 , height: 200)
            .padding(.top)
            .aspectRatio(contentMode: .fit)
            .frame(maxWidth: 200, maxHeight: 200)
            .transition(.slide)
        HStack {
            
            Spacer()
            Button(action: {showAddPhotoSheet.toggle()}) {
                Label("Take a photo", systemImage: "camera")
                    .foregroundColor(Color("BrandPrimary"))
            }
            .sheet(isPresented: $showAddPhotoSheet){
                ImagePicker(imageSelected: $imgSelected, sourceType: $sourceType)
               // ImageViewPicker()
            }
            
            
              Button(action: { showPhotoSheet = true }) {
                Label("Choose photo", systemImage: "photo.fill")
                      .foregroundColor(Color("BrandPrimary"))

              }
                .fullScreenCover(isPresented: $showPhotoSheet) {
                PhotoPicker(filter: .images, limit: 1) { results in
                  PhotoPicker.convertToUIImageArray(fromResults: results) { (imagesOrNil, errorOrNil) in
                    if let error = errorOrNil {
                      print(error)
                    }
                    if let images = imagesOrNil {
                      if let first = images.first {
                        print(first)
                       // image = first
                          imgSelected = first
                      }
                    }
                  }
                }
                .edgesIgnoringSafeArea(.all)
              }

            
            
            Spacer()
            
        }

       }
    
    
}


struct SecondVIew: View {
    
 
    var body: some View {
PickAPhoto()
      }
    func getImage(){
// I want to get the image here
}

}

CodePudding user response:

Move the imgSelected state up and pass a binding down.

struct SecondVIew: View {
    @State var imgSelected: UIImage = UIImage(named: "addCameraImg")!
 
    var body: some View {
        PickAPhoto(imgSelected: $imgSelected)
    }
}

struct PickAPhoto: View {
    @Binding var imgSelected: UIImage
  • Related