我正在使用库存 AsyncImage 将图像从 Firebase 存储加载到我的应用程序。我使用两个完全相同的代码片段来加载相同的图像,但是应用程序仅在只有第一个片段时编译,第二个片段会导致崩溃。它们之间的唯一区别是我想使用第二个在 ForEach 循环中显示几个相同的图像。
第一个片段:
HStack {
if let profilePicturePhotoURL = profileViewModel.profilePicturePhotoURL {
AsyncImage(url: profilePicturePhotoURL) { phase in
if let image = phase.image {
image
.resizable()
} else {
Image(uiImage: UIImage(named: "blank-profile-hi")!)
}
}
} else {
Image(uiImage: UIImage(named: "blank-profile-hi")!)
}
Text("What do you want to share?")
.frame(width: screenWidth * 0.6, height: screenHeight * 0.1)
}
.padding(.leading, screenWidth * 0.05)
第二个片段:
if homeViewModel.posts != nil {
ForEach(homeViewModel.posts!) { post in
VStack {
Rectangle()
.foregroundColor(Color(uiColor: .systemGray6))
.frame(width: screenWidth, height: screenHeight * 0.02)
HStack {
if let profilePicturePhotoURL = profileViewModel.profilePicturePhotoURL {
AsyncImage(url: profilePicturePhotoURL) { phase in
if let image = phase.image {
image
.resizable()
} else {
Image(uiImage: UIImage(named: "blank-profile-hi")!)
}
}
} else {
Image(uiImage: UIImage(named: "blank-profile-hi")!)
}
只离开
Image(uiImage: UIImage(named: "blank-profile-hi")!)
因为第二个片段的图像使图像正确显示。