I think it is about async operation. I come up with this solution;
in view:
@State private var image: UIImage? = nil
in body:
TabView() {
Image(uiImage: image ?? UIImage(named: "defaultImage")!)
.resizable()
.clipped()
.frame(width: 250, height:200, alignment: .topLeading)
.onAppear(perform: {
fetchImage()
})
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
*I used "UIImage(named: "defaultImage")!" and "width: 250, height:200" but you can replace them with yours.
in view:
private func fetchImage() {
if let url = URL(string: imageUrl) {
KingfisherManager.shared.retrieveImage(with: url) { result in
let image = try? result.get().image
if let image = image {
self.image = image
}
}
}
}
Another solution without using Kingfisher would be like;
TabView() {
if let url = URL(string: imageUrl) {
if let imageData: NSData = NSData(contentsOf: url) {
if let image = UIImage(data: imageData as Data) {
Image(uiImage: image)
.resizable()
.clipped()
.frame(width: 250, height:200, alignment: .topLeading)
}
}
}
}
.tabViewStyle(PageTabViewStyle(indexDisplayMode: .never))
This works well.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…