Goal: Have a button, with an image (system image) in it. User taps, button, selects image from photo library, and button now appears with selected image.
I have the code to select the Image from UIImagePicker. I can even get the code so that the image updates another area in the VStack (like the apple tutorial and as Brian Advent shows).
ISSUE:
However, I can’t get the button to update it’s self.
I get either a Blue square where the button is and not the image, or I get that false error “Int is not convertible to CGFloat”
I’ve tried using .aspectRation(1, contentMode: .fit) and a multitude of other things, yet can NOT get this to work.
Code:
CODE THAT WORKS:
import SwiftUI
struct ContentView: View {
@State var showingImagePicker = false
@State var image: Image? = nil
var body: some View {
VStack{
if (image == nil) {
Image(systemName: "square.and.arrow.down.fill")
.resizable()
.frame(width: 150, height: 150)
Text("No Image specified\n Select Image")
}
else {
image?
.resizable()
.frame(width: 150, height: 150)
}
Button("choose Image") {
self.showingImagePicker.toggle()
}
}.sheet(isPresented: $showingImagePicker, content: {
ImagePicker.shared.view
}).onReceive(ImagePicker.shared.$image) {
image in self.image = image
}
CODE THAT DOES NOT WORK
import SwiftUI
struct ContentView: View {
@State var showingImagePicker = false
@State var image: Image? = nil
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 {
/*THIS IS THE SECTION I GET THE ERROR: can’t convert int to cgfloat
Image(uiImage: image)
.resizable()
.frame(width: 150, height: 150)
//.padding(.bottom)
*/
}
}
}.sheet(isPresented: $showingImagePicker, content: {
ImagePicker.shared.view
}).onReceive(ImagePicker.shared.$image) {
image in self.image = image
}
}
}
I’ve been at this for weeks now, and have become extremely frustrated with it and I’m about to give up on swiftUI and just write it off as an unuseable language. So any help would be appreciated