How do I change an image in a button

All, I've got the ability to select images figured out. As well as set images in a button statcally.

However, I want to be able to change an image in a button after an image is selected.

I have been at this for days and can't seem to figure it out, or find any answers to my question.

Any help is appreciated


The code I have is:

Button(action: {

if(self.ImageSelectedFlag == false) {

self.showImages.toggle()

}

else {

?????????????

}

})

{

Image(systemName: "question")

}

.sheet(isPresented: $IsShowingImages, content: {

ImagePicker(isPresented: self.$IsShowingImages, ImageSelected: self.ButtonImage, ImageSelectedFlag: self.$ImageSelectedFlag)

})

Ok,

realize the mistake in the above code.


However, still doesn't work.

I get the infamous can't convert int to cgfloat error.


Code should be down in the lable section:

var body: some View {

VStack{

Button(action: {

if (self.image == nil) {

self.showingImagePicker.toggle()

}

else {

print("Image Selected")

//New_more_other code here

}

})

{

if (image == nil) {

VStack {

Image(systemName: "square.and.arrow.down.fill")

.resizable()

.frame(width: 150, height: 150)

Text("No Image specified\n Select Image")

}

}

else {

Image(uiImage: image)

.resizable()

.frame(width: 150, height: 150)

}

}

}.sheet(isPresented: $showingImagePicker, content: {

ImagePicker.shared.view

}).onReceive(ImagePicker.shared.$image) {

image in self.image = image

}

}


ANY help would be appreciated as I can not tell what I'm doing wrong here.

A few comments first:

- should format the code with <> tool, which gives line references:


var body: some View {
        VStack{
          
            Button(action: {
                if (self.image == nil) {
                    self.showingImagePicker.toggle()
                }
                else {
                    print("Image Selected")
                    //New_more_other code here
                }
            })
            {
                if (image == nil) {
                    VStack {
                        Image(systemName: "square.and.arrow.down.fill")
                            .resizable()
                            .frame(width: 150, height: 150)
                        Text("No Image specified\n      Select Image")
                    }
                }
                else {
                        Image(uiImage: image)
                            .resizable()
                            .frame(width: 150, height: 150)
                }
            }
        }.sheet(isPresented: $showingImagePicker, content: {
            ImagePicker.shared.view
        }).onReceive(ImagePicker.shared.$image) {
            image in self.image = image
        }
    }


- where do you get the error ? can't convert int to cgfloat.


https://stackoverflow.com/questions/56820779/how-can-i-create-a-button-with-image-in-swiftui

How do I change an image in a button
 
 
Q