[ad_1]
I have a profile picture that I am attempting to show a default image until a user selects a picture from a picker (say that three times fast). In WebImage, I point to a view Model, which connects to my database to grab the user image. If no image exists I currently just have it on an empty string.
For reference, this is the line that serves up the image
WebImage(url: URL(string: vm.userModel?.profilePictureURL ?? “”)
I tried adding a bool that updated on the onChange function to true and tried wrapping the webImage in an IF statement, but the view didn’t update correctly.
import SwiftUI
import Firebase
import FirebaseFirestore
import SDWebImageSwiftUI
struct ProfilePicture: View {
@ObservedObject var vm = DashboardLogic()
@State private var showingImagePicker = false
@State private var inputImage: UIImage?
var body: some View {
ZStack (alignment: .topTrailing){
VStack{
if let inputImage = inputImage {
Image(uiImage: inputImage)
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width:150, height: 150)
.clipShape(Circle())
} else{
WebImage(url: URL(string: vm.userModel?.profilePictureURL ?? ""))
.resizable()
.aspectRatio(contentMode: .fill)
.frame(width:150, height: 150)
.clipShape(Circle())
}
Button(action: {
showingImagePicker = true
}){
Image(systemName:("plus.circle.fill"))
.resizable()
.aspectRatio(contentMode: .fill)
.foregroundColor(Color("ButtonTwo"))
.frame(width:30, height:25)
.contentShape(Rectangle())
}
//PRESENT PICKER
}
.sheet(isPresented: $showingImagePicker){
EditorImagePicker(image: $inputImage)
}
}
//SAVE IMAGE TO DATABASE (FIREBASE)
.onChange(of: inputImage, perform: { _ in
persistImageToStorage() //call to save function
})
}
}
}
[ad_2]