|
I'm writing an app that reads a JSON data file. One of the fields in the JSON file is a URL to an image. When I display parts of the JSON file, I would also like to display the image . Whats the best approach to do this? |
|
|
|
SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today! Sponsor Hacking with Swift and reach the world's largest Swift community! |
|
Make a file with this code from Hacking with Swift plus
then use in your view
Hope that helps |
|
|
|
Didn't seem to like @StateObject, but I just replaced that with @ObservedObject and the code seems to work. Thanks aain. |
|
|
|
This works, but there's no caching. Has anyone implemented a caching solution they'd like to share? :) |
|
Hi, thank you for this, it works good. But do you have a trick to reload if needed ? For now i reload the parents view and it works but is there a better way ? |
|
This works perfectly when given a URL upon initialization. However, if my URL is part of a @State or @ObservedObject that changes, the image does not update, even when the view is refreshed. Is there a way to pull in the url every time selectImage is executed? |
|
|
|
@andycairnsccl This is what i finally use to have this behavior: i use SDWebImage https://github.com/SDWebImage/SDWebImageSwiftUI It works really good but have the same refresh problem BUT the following trick works : use an image url with this format: http://mywebsite/images/myimage.jpg?time=1606039247 (use current unix timestamp). Then you just have to refresh the url (the timestamp) every time you want to refresh the image, and that´s it ! |
|
|
|
@a3igner Were you using SwiftUI previews? As of Xcode 12.4 it seems like networking is disabled in the preview environment so the web image won't load in the preview window. When I run it on the simulator the remote image loads as expected though. It's also possible that the string URL passed in was invalid (I didn't like the fatal error when the URL couldn't be created - I opted to use an actual URL type in the initializers to make things cleaner) |
SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!
Sponsor Hacking with Swift and reach the world's largest Swift community!
This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.
All interactions here are governed by our code of conduct.
Link copied to your pasteboard.