我有一个可能包含图像的游戏对象。每当找到游戏的图像URL时,都应创建GameImage对象的新实例。然后它将获取图像并填充UIImage属性。发生这种情况时,应该更新UI以显示图像。
class Game: ObservableObject {
@Published var image: GameImage?
}
class GameImage: ObservableObject {
let url: URL
@Published var image: UIImage?
private var cancellable: AnyCancellable?
init(url: URL) {
self.url = url
}
func fetch() {
self.cancellable = URLSession.shared.dataTaskPublisher(for: self.url)
.map { UIImage(data: $0.data) }
.replaceError(with: nil)
.receive(on: DispatchQueue.main)
.sink(receiveValue: { [weak self] (image) in
guard let self = self else { return }
self.image = image
print(self.url)
print(self.image)
})
}
func cancel() {
cancellable?.cancel()
}
deinit {
cancel()
}
}
struct ContentView: View {
@StateObject var game = Game()
var body: some View {
VStack {
if let image = game.image?.image {
Image(uiImage: image)
} else {
Text("No image.")
}
}
.onAppear(perform: {
guard let gameImageURL = URL(string: "https://cf.geekdo-images.com/itemrep/img/oVEpcbtyWkJjIjk1peTJo6hI1yk=/fit-in/246x300/pic4884996.jpg") else { return }
game.image = GameImage(url: gameImageURL)
game.image!.fetch()
})
}
}
问题是。提取完成后,调试控制台将显示该图像包含一个UIImage。但是,UI不会更新以显示图像。我在这里想念什么?
答案 0 :(得分:1)
有比链接background
更简单的解决方案,只需将相关部分分成独立的子视图...,所有这些都将自动运行。
这是可行的方法。在Xcode 12 / iOS 14上进行了测试。
ObservableObject