In case anyone else is having a similar issue as I am, I have managed to solve it. I still do not know the cause, so I went ahead and filed a bug report, but I do know the fix. Do not fret, I will not leave you in the dark for when you inevitably stumble upon this in ten years time just to see "Solved it!".
As much as I would like to take credit for it, credit should actually go to @timbretimbre on StackOverflow for his superb recommendation to streamline my code. Here's the link to the original question just for thoroughness' sake: https://stackoverflow.com/questions/76524832/image-view-is-rendering-in-xcode-preview-but-not-on-device
First, I completely deleted the "@State var image" variable at the top. Next, in the ImageLoader class, replace the data variable with a normal, @Published variable and initialize it to a Data() object. Basically, just delete the didSet code block and add @Published. And then finally, remove the .onReceive bit from the Image view up top and set the uiImage attribute of the Image View to "UIImage(data: imageLoader.data)!"
Here's a summary:
Remove the "image" variable:
struct ImageView: View {
@ObservedObject var imageLoader: ImageLoader
init(withUrl url: String) {
imageLoader = ImageLoader(urlString: url)
}
Refactor the data variable inside the ImageLoader class:
class ImageLoader: ObservableObject {
var didChange = PassthroughSubject<Data, Never>()
@Published var data = Data()
Refactor the Image View:
Image(uiImage: UIImage(data: imageLoader.data)!)
.resizable()
.aspectRatio(contentMode: .fit)
Hope this helps someone!
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: