为什么在SwiftUI教程中没有显示图片对象?

问题描述

我一直在尝试将布局更改为Apple提供的WatchLandmarks教程的布局,该教程用于为手表(https://developer.apple.com/tutorials/swiftui/creating-a-watchos-app)制作SwiftUI应用程序,但我不知道为什么没有为我加载图像。在完成的教程代码中,它工作得很好,唯一的区别是我对LandmarkList和LandmarkRow的布局进行了更改。

更改为LandmarkList:

struct LandmarkList<DetailView: View>: View {
    @EnvironmentObject private var userData: UserData
    
    let detailViewProducer: (Landmark) -> DetailView
    
    var body: some View {
        VStack(alignment: .leading){
            Text(" Landmarks")
                .font(.system(size: 16))
            ScrollView(.horizontal,showsIndicators:false){
                HStack(alignment: .center){
                    ForEach(userData.landmarks) { landmark in
//                        if !self.userData.showFavoritesOnly || landmark.isFavorite {
                            NavigationLink(
                                destination: self.detailViewProducer(landmark).environmentObject(self.userData)) {
                                LandmarkRow(landmark: landmark)
                                    .frame( maxWidth: 70,maxHeight: 70,alignment: .leading)
                            }
//                        }
                    }
                }
                
            }.frame(minWidth: 0,maxWidth: .infinity,maxHeight: 100,alignment: .center)//.frame(width:150,height: 100)
            Text(self.userData.landmarks[0].name)
                .frame(minWidth: 0,minHeight: 24,alignment: .center)
            HStack(spacing: 32){
                Button(action: { print("first button") }) {
                    Text("P")
                }.frame(minWidth: 20,maxWidth:50,minHeight: 20,maxHeight:50)
                    .background(Color.orange)
                    .clipShape(Circle())
                    .overlay(Circle().stroke(Color.clear,lineWidth: 4))
                    .shadow(radius: 10)
                Button(action: { print("second button")  }) {
                    Text("S")
                }.frame(minWidth: 20,lineWidth: 4))
                    .shadow(radius: 10)
            }.frame(minWidth: 0,alignment: .center)
//        List {
//            Toggle(isOn: $userData.showFavoritesOnly) {
//                Text("Show Favorites Only")
//            }
//
//            ForEach(userData.landmarks) { landmark in
//                if !self.userData.showFavoritesOnly || landmark.isFavorite {
//                    NavigationLink(
//                    destination: self.detailViewProducer(landmark).environmentObject(self.userData)) {
//                        LandmarkRow(landmark: landmark)
//                    }
//                }
//            }
//        }
//        .navigationBarTitle(Text("Landmarks"))
        }.frame(maxWidth: .infinity,minHeight:200,maxHeight: .infinity,alignment: .leading)
    }
}

更改为LandmarkRow

struct LandmarkRow: View {
    var landmark: Landmark

    var body: some View {
        HStack {
            landmark.image
                .resizable()
                .frame(width: 50,height: 50)
                .cornerRadius(8.0)
//            Text(landmark.name)
//            Spacer()
//
//            if landmark.isFavorite {
//                Image(systemName: "star.fill")
//                    .imageScale(.medium)
//                    .foregroundColor(.yellow)
//            }
        }
    }
}

我在做什么错了?

解决方法

尝试在图像上调用.renderingMode(.original)

您的图像资产有两种渲染模式:

  • 原始
  • 模板

原始模式将按原样显示图像资产,而模板模式会将图像的所有非透明部分变成一种可以设置的颜色。默认颜色是黑色。 注意:Apple文档中没有可用的概述。 在本教程中,您将学习SwiftUI中的渲染模式以及使用方式。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...