Challenge - 5 Problems
Image Loading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate2:00remaining
What will this SwiftUI code display?
Consider this SwiftUI view that loads an image from a URL asynchronously. What will the user see when the view appears?
iOS Swift
import SwiftUI struct ContentView: View { let url = URL(string: "https://example.com/image.png")! var body: some View { AsyncImage(url: url) { image in image.resizable().scaledToFit() } placeholder: { ProgressView() } } }
Attempts:
2 left
💡 Hint
Think about what AsyncImage does while waiting for the image to load.
✗ Incorrect
AsyncImage shows the placeholder view (ProgressView) while loading the image from the URL. Once loaded, it displays the image resized to fit the view.
📝 Syntax
intermediate2:00remaining
Which code snippet correctly loads an image from a URL in UIKit?
You want to load an image from a URL asynchronously and display it in a UIImageView. Which code snippet is correct?
Attempts:
2 left
💡 Hint
Remember that loading data from the network should be asynchronous and UI updates on the main thread.
✗ Incorrect
Option B uses URLSession to fetch data asynchronously and updates the UIImageView on the main thread. Option B blocks the main thread and can crash. Options C and D misuse UIImage initializers.
❓ lifecycle
advanced2:00remaining
What happens if you load an image from a URL in viewDidLoad without caching?
In a UIViewController, you load an image from a URL in viewDidLoad every time the view appears. What is a likely consequence?
Attempts:
2 left
💡 Hint
Think about how network requests behave if you do not store or cache the image data.
✗ Incorrect
Without caching, loading the image in viewDidLoad causes a network request each time the view loads, which wastes data and slows the app.
🔧 Debug
advanced2:00remaining
Why does this SwiftUI AsyncImage code never show the image?
This SwiftUI code uses AsyncImage but the image never appears, only the placeholder spinner. What is the problem?
iOS Swift
AsyncImage(url: URL(string: "https://example.com/image.png")) { image in image.resizable() } placeholder: { ProgressView() }
Attempts:
2 left
💡 Hint
Think about how SwiftUI views need explicit size or scaling to be visible.
✗ Incorrect
Without a size or scaling modifier like scaledToFit or a frame, the image has zero size and does not appear, even though it loads.
🧠 Conceptual
expert3:00remaining
What is the best practice for loading images from URLs in a production iOS app?
Which approach is best for loading images from URLs in a production iOS app to ensure good performance and user experience?
Attempts:
2 left
💡 Hint
Think about caching, smooth loading, and reusability in real apps.
✗ Incorrect
Third-party libraries provide optimized async loading, caching, and placeholder support, improving performance and user experience.