0
0
iOS Swiftmobile~20 mins

Image loading from URL in iOS Swift - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Image Loading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2: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()
    }
  }
}
AA loading spinner appears first, then the image loads and fits the view.
BThe image appears immediately without any loading indicator.
CThe view shows a blank space with no image or indicator.
DThe app crashes because AsyncImage requires a different initializer.
Attempts:
2 left
💡 Hint
Think about what AsyncImage does while waiting for the image to load.
📝 Syntax
intermediate
2: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?
A
let url = URL(string: "https://example.com/image.png")!
let data = try? Data(contentsOf: url)
imageView.image = UIImage(data: data!)
B
let url = URL(string: "https://example.com/image.png")!
URLSession.shared.dataTask(with: url) { data, _, _ in
  if let data = data {
    DispatchQueue.main.async {
      imageView.image = UIImage(data: data)
    }
  }
}.resume()
C
let url = URL(string: "https://example.com/image.png")!
imageView.image = UIImage(contentsOfFile: url.path)
D
let url = URL(string: "https://example.com/image.png")!
imageView.image = UIImage(named: url.absoluteString)
Attempts:
2 left
💡 Hint
Remember that loading data from the network should be asynchronous and UI updates on the main thread.
lifecycle
advanced
2: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?
AThe app crashes because images cannot be loaded in viewDidLoad.
BThe image never loads because viewDidLoad is too early in the lifecycle.
CThe image loads once and is cached automatically by the system, so no extra network calls happen.
DThe image reloads every time the view appears, causing unnecessary network usage and slower UI.
Attempts:
2 left
💡 Hint
Think about how network requests behave if you do not store or cache the image data.
🔧 Debug
advanced
2: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()
}
AThe image is not scaled or sized, so it has zero size and is invisible.
BThe URL string is invalid and causes AsyncImage to fail silently.
CAsyncImage requires a .frame modifier to work properly.
DThe placeholder view blocks the image from appearing.
Attempts:
2 left
💡 Hint
Think about how SwiftUI views need explicit size or scaling to be visible.
🧠 Conceptual
expert
3: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?
ALoad images synchronously with Data(contentsOf:) to simplify code.
BAlways use URLSession dataTask directly and reload images every time the view appears.
CUse a third-party library like SDWebImage or Kingfisher that handles async loading, caching, and placeholder images.
DUse UIImage(named:) with the URL string to load images efficiently.
Attempts:
2 left
💡 Hint
Think about caching, smooth loading, and reusability in real apps.