以下是我的看法:
struct EditProfile: View {
@State var bio = UserDefaults.standard.string(forKey: "bio") ?? "Edit your bio"
@ObservedObject var storage = FirebaseStorage()
@State var text = ""
let photos = ["img1", "img2", "img3"]
var body: some View {
VStack {
Text("Photos")
.font(Font.system(size: 21))
.fontWeight(.bold)
HStack {
ForEach(photos, id: \.self){ img in
EditableCircleImage(kfImage: self.storage.makeCircleImage(str: img))
}
}
.background(
RoundedRectangle(cornerRadius: 10)
.fill(Color.white)
.shadow(color: .gray, radius: 1, x: 0, y: 1)
)
Text("Bio")
.font(Font.system(size: 21))
.fontWeight(.bold)
MultiTextField(txt: $text)
.padding(10)
.cornerRadius(20.0)
.background(
RoundedRectangle(cornerRadius: 10)
.fill(Color.white)
.shadow(color: .gray, radius: 1, x: 0, y: 1)
)
.border(Color.gray.opacity(0.5), width: 1)
}.padding(20).background(Color.gray.opacity(0.1))
}
}
下面是EditableCircleImage()
:
struct EditableCircleImage: View {
let kfImage: KFImage
var body: some View {
ZStack {
kfImage
.resizable()
.scaledToFit()
.frame(height: 200)
.clipShape(Circle())
.overlay(Circle().stroke(Color.orange, lineWidth: 2))
Button(action: {}){
Image(systemName: "pencil.circle.fill")
.resizable()
.scaledToFit()
.frame(height: 40)
.foregroundColor(.gray)
}.offset(x: 40, y: 50)
}
}
}
下面是返回图像的模型:FireBaseStorage.swift
:
class FirebaseStorage: ObservableObject {
let storage = Storage.storage()
let uid = UserAuth().uid ?? "<uid>"
@Published var img1 = UserDefaults.standard.string(forKey: "img1") ?? "<img1>"
func makeCircleImage(str: String) -> KFImage {
if let url = UserDefaults.standard.string(forKey: str) {
print("not nil: \(str)")
return KFImage(URL(string: "http://app-23c8s.appspot.com.storage.googleapis.com/users/\(uid)/\(url)"))
}
else {
print("is nil: \(str)")
return KFImage(source: nil)
}
}
}
上面模型中的print语句一遍又一遍地打印,即使在初始呈现后没有任何值发生变化。 知道为什么吗?
不断打印:
not nil: img1
is nil: img2
is nil: img3
not nil: img1
is nil: img2
is nil: img3
not nil: img1
is nil: img2
is nil: img3
not nil: img1
is nil: img2
is nil: img3
not nil: img1
is nil: img2
is nil: img3
not nil: img1
is nil: img2
is nil: img3
not nil: img1
is nil: img2
is nil: img3
not nil: img1
is nil: img2
is nil: img3
not nil: img1
is nil: img2
is nil: img3
not nil: img1
is nil: img2
is nil: img3
not nil: img1
is nil: img2
is nil: img3
not nil: img1
is nil: img2
is nil: img3
not nil: img1
is nil: img2
is nil: img3
not nil: img1
is nil: img2
is nil: img3
not nil: img1
is nil: img2
is nil: img3
not nil: img1
is nil: img2
is nil: img3
not nil: img1
is nil: img2
is nil: img3
not nil: img1
is nil: img2
is nil: img3
not nil: img1
is nil: img2
is nil: img3
not nil: img1
is nil: img2
is nil: img3
not nil: img1
is nil: img2
is nil: img3
更新:
我改了:
ForEach(photos, id: \.self){ img in
EditableCircleImage(kfImage: self.storage.makeCircleImage(str: img))
}
至
EditableCircleImage(kfImage: self.storage.makeCircleImage(str: "img1"))
现在它打印:
not nil: img1
not nil: img1
not nil: img1
not nil: img1
not nil: img1
not nil: img1
not nil: img1
not nil: img1
not nil: img1
not nil: img1
not nil: img1
not nil: img1
not nil: img1
not nil: img1
not nil: img1
not nil: img1
not nil: img1
not nil: img1
移除FirebaseStorage类与ObservableObject的一致性,并从@ObservedObject var storage=FirebaseStorage()
中移除属性包装器@ObservedObject。 也删除@Published属性包装。 运行它,如果问题仍然存在,请告诉我。 顺便说一下,我不清楚为什么要使类符合ObservableObject,以及为什么要在这个特定的用例中使用属性包装器。 如果我遗漏了什么,一定要让我知道。 假设这篇文章包含了您正在谈论的问题的整个代码。 希望这能帮到你。
看来
func makeCircleImage(str: String) -> KFImage { ... }
是一个异步函数,特别是KFImage(。。)。 您可以在以下情况下使用此选项:
ForEach(photos, id: \.self){ img in
EditableCircleImage(kfImage: self.storage.makeCircleImage(str: img))
}
这大概就是问题的源头。 你真的应该等到你拥有了所有的图像之后再使用它们,尤其是在一个视图中。