我正在尝试将图像从 URL 加载到小部件。在我最近制作的另一个项目中,我已经这样做了。现在我正在尝试执行相同的方法并看到一些奇怪的错误Argument type 'URL' does not conform to expected type 'Decoder'
和Cannot convert value of type 'ExampleWidgetExtension.Data' to expected argument type 'Foundation.Data'
. 我的另一个项目工作得很好,我Codable
像以前一样使用数据。除了图像之外的一切似乎都在工作。
struct ExampleWidgetEntryView : View {
var entry: Provider.Entry
var body: some View {
VStack (alignment: .leading, spacing: 5) {
ForEach(entry.model?.example.results?.prefix(1) ?? [], id: \.id) { example in
/*
Text(example.data.title ?? "")
.fontWeight(.bold)
.font(Font.system(.caption))
.lineLimit(1)
*/
if let url = URL(string:example.data.url ?? ""),
let imageData = try? Data(from: url),
let uiImage = UIImage(data: imageData) {
Image(uiImage: uiImage)
.resizable()
.frame(width: 30, height: 30)
}
}
}
.padding(.all, 10)
}
}