URLSession.shared .dataTaskPublisher(for: URL(string: "https://picsum.photos/300/600")!) .map(\.data) .compactMap(UIImage.init) /// Schedule to receive the sink closure on the /// main dispatch queue. .receive(on: DispatchQueue.main, options: nil) .sink { _ in print("Image loading completed") } receiveValue: { image in self.image = image }.store(in: &cancellables)
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter