Post

Replies

Boosts

Views

Activity

Reply to How do I implement @StateObject in AsyncImage?
Okay, I'll be honest I'm pretty new at this and a lot of what is described is beyond my understanding! Do I have to initialize separately where the AsyncImage is called in my views? And do I have to change the init in my ImageLoader also? class ImageLoader: ObservableObject {     @Published var image: UIImage?     private let url: URL     private var cancellable: AnyCancellable?     deinit {         cancellable?.cancel()     }     init(url: URL) {         self.url = url     }     func load() {         cancellable = URLSession.shared.dataTaskPublisher(for: url)             .map { UIImage(data: $0.data) }             .replaceError(with: nil)             .receive(on: DispatchQueue.main)             .assign(to: \.image, on: self)     }     func cancel() {         cancellable?.cancel()     } } struct AsyncImage<Placeholder: View>: View {     @StateObject private var loader: ImageLoader     private let placeholder: Placeholder?     init(url: URL, placeholder: Placeholder? = nil) {         loader = ImageLoader(url: url)         self.placeholder = placeholder     }     var body: some View {         image             .onAppear(perform: loader.load)             .onDisappear(perform: loader.cancel)     }     private var image: some View {         Group {             if loader.image != nil {                 Image(uiImage: loader.image!)                     .resizable()                     .aspectRatio(contentMode: .fit)             } else {                 Image("BlankCardImage")                     .resizable()                     .aspectRatio(contentMode: .fit)             }         }     }
Jun ’20
Reply to How do I implement @StateObject in AsyncImage?
Thanks @OOPer! I think I'm getting closer. Here's how I'm using the Async image in my view, but the errors aren't any help.                 AsyncImage(                     loader: ImageLoader()                     url: URL(string: card.images!["normal"]!)!,                     placeholder: Text("")                 ) I'm at a loss where/how to initialize it. The error reads "'ImageLoader' cannot be constructed because it has no accessible initializers"
Jun ’20
Reply to Data flow and managed objects in SwiftUI
Thanks DelawareMathGuy, this is really helpful. Specifying the environment solved the issue of managing object from the detail view. As far as passing the right entry to the detail view I tried both methods without success. The off-loading of the entry to a local variable is how I've done it in the past but Ive never done it with Core Data. Right now when I do @ObservedObject it says ".self is immutable" in the view body. When I put @State SceneDelegate begs for a "chosenEntry" in the call to ContentView. I cant forge out how to declare an Entry type without it bugging out. I appreciate the tips
Jul ’20
Reply to Data flow and managed objects in SwiftUI
Okay I've cracked it open and tried out the method of passing in the data rather than the object itself by offloading to a struct. That definitely solves the issue, optically, but I need to refactor editing of deleting the item from the modal. I'm sure i'll figure it out tonight. If I get stuck again or have further questions I'll be back! Regardless, thanks DMG for the help and great ShoppingList project as a resource!
Jul ’20
Reply to How to fetch an image in the Live Activity (ActivityKit)
Hi, I was doing something similar using AppStorage and AppGroup to pass the fetched image data from the main app to the Live Activity and Dynamic Island. It previously worked in iOS 16.1 beta 1, and beta 3 but hasn't resulted in rendering the image for the last two betas as well as the RC. I tried the above solution as well, and the data is correctly fetched and passed, but the Live Activity will not render Image(uiImage: UIImage(data: data)). It will render any SF Symbol or static image in the asset catalogue just fine, but not usable otheriwse. Can someone help determine a way to render a fetched image in a Live Activity or Dynamic Island? Is there a trick to getting Image(uiImage: UIImage(data: data)) to render properly?
Oct ’22