图像选择器 SwiftUI

Image Picker SwiftUI

提问人:viet anh nguyen 提问时间:10/19/2023 更新时间:10/19/2023 访问量:35

问:

我的代码有问题:

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) 当用户点击按钮并选择“图库中的图像”时,该按钮将显示用户选择的图像。但之后,如果用户删除图像,该按钮会继续显示用户之前选择的图像。当用户没有选择图像时,如何重置按钮标签,就像开始一样

Firebase macOS SwiftUI ImagePicker

评论

0赞 vacawama 10/19/2023
当用户点击删除时怎么办?self.image = UIImage()
0赞 workingdog support Ukraine 10/19/2023
无关紧要,但我相信它总是会继续进行,所以没有意义。isUpdateSuccessfultrue.alert(isPresented: $isUpdateSuccessful)if isUpdateSuccessful {...} else {...}
0赞 viet anh nguyen 10/20/2023
我尝试设置,但它仍然不起作用。其他任何想法self.image = UIImage()
0赞 viet anh nguyen 10/20/2023
我试图创建一个函数来重置 AuthViewModel 中的 photoURL 值并设置它终于工作了,感谢您的帮助self.image = UIImage()

答: 暂无答案