提问人:viet anh nguyen 提问时间:10/19/2023 更新时间:10/19/2023 访问量:35
图像选择器 SwiftUI
Image Picker SwiftUI
问:
我的代码有问题:
import SwiftUI
struct EditUserView: View {
@State var isUpdateSuccessful = false
@EnvironmentObject var storageManager: StorageManager
@EnvironmentObject var viewModel: AuthViewModel
@State var fullName = ""
@State var email = ""
@State var avatarURL = ""
@State var password = ""
@State var image = UIImage()
@State var showSheet = false
var body: some View {
if let user = viewModel.currentUser {
Spacer()
VStack {
VStack {
Button {
showSheet.toggle()
} label: {
if user.photoURL == "" {
Image(uiImage: self.image)
.resizable()
.cornerRadius(50)
.frame(width: 200, height: 200)
.background(
Text(user.initials)
.font(.system(size: 80))
.fontWeight(.semibold)
.foregroundStyle(Color(.white))
.frame(width: 200, height: 200)
.background(Color(.systemGray3))
.clipShape(Circle())
)
.aspectRatio(contentMode: .fill)
.clipShape(Circle())
}
else {
AsyncImage(url: URL(string: user.photoURL)){ image in
image
.image?.resizable()
.scaledToFill()
.frame(width: 200, height: 200)
.cornerRadius(34)
.clipShape(Circle())
.aspectRatio(contentMode: .fill)
}
}
}
Button {
Task {
try await storageManager.deleteAvatarImage(photoURL: user.photoURL)
try await viewModel.updateUser(withEmail: email, fullName: fullName, password: password, photoURL: "")
}
} label: {
Text("Delete Image")
}
}
.fullScreenCover(isPresented: $showSheet, onDismiss: nil) {
ImagePicker(sourceType: .photoLibrary, selectedImage: self.$image)
}
}
Spacer()
VStack(spacing: 20) {
InfoInputView(text: $fullName, title: "Full Name", content: user.fullName)
InfoInputView(text: $email, title: "Email", content: user.email)
}
.padding(.horizontal, 40)
Spacer()
VStack {
Button {
Task {
do {
let photoURL = await storageManager.uploadImageAndGetURL(image)
try await viewModel.updateUser(withEmail: email, fullName: fullName, password: password, photoURL: photoURL)
// Cập nhật thành công, hiển thị thông báo
isUpdateSuccessful = true
} catch {
// Xử lý lỗi nếu cập nhật thất bại
isUpdateSuccessful = false
}
}
} label: {
HStack {
Spacer()
Text("Update Profile")
.font(.system(size: 16, weight: .bold))
Spacer()
}
.foregroundStyle(Color(.white))
.padding(.vertical)
.background(Color(.black))
.cornerRadius(32)
.padding(.horizontal)
.shadow(radius: 15)
}
.alert(isPresented: $isUpdateSuccessful) {
if isUpdateSuccessful {
Alert(title: Text("Update successful"),
message: Text("User profile has been updated."),
dismissButton: .default(Text("OK"))
)
}
else {
Alert(title: Text("Update failed"),
message: Text("User profile hasn't been updated."),
dismissButton: .default(Text("OK"))
)
}
}
}
}
}
}
我正在使用此视图来编辑用户信息。更改用户全名和电子邮件的信息工作正常。上传和删除头像也正常工作。如果 user.photoURL 为空,则视图将如下所示 (https://i.stack.imgur.com/Ou0KS.png)](https://i.stack.imgur.com/Ou0KS.png) 当用户点击按钮并选择“图库中的图像”时,该按钮将显示用户选择的图像。但之后,如果用户删除图像,该按钮会继续显示用户之前选择的图像。当用户没有选择图像时,如何重置按钮标签,就像开始一样
答: 暂无答案
评论
self.image = UIImage()
isUpdateSuccessful
true
.alert(isPresented: $isUpdateSuccessful)
if isUpdateSuccessful {...} else {...}
self.image = UIImage()
self.image = UIImage()