Junior — Middle
How do you load an image from a remote source by its URL?
sobes.tech AI
Answer from AI
To load an image from a remote URL in iOS, the URLSession class is usually used for asynchronous data loading, and then UIImage is created from the received data. Example:
if let url = URL(string: "https://example.com/image.png") {
URLSession.shared.dataTask(with: url) { data, response, error in
if let data = data, let image = UIImage(data: data) {
DispatchQueue.main.async {
imageView.image = image
}
}
}.resume()
}
For simplification and caching, third-party libraries such as SDWebImage or Kingfisher are often used, which allow you to load and display an image with a single line of code.