Last active
October 19, 2020 19:49
-
-
Save mitnick78/fe3265591b81617a43ab739db27102d2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import Combine | |
class ImageLoader: ObservableObject { | |
var downloadedImage: UIImage? | |
let didChange = PassthroughSubject<ImageLoader?, Never>() | |
func load(url: String) { | |
guard let imageURL = URL(string: url) else { | |
fatalError("ImageURL is not correct!") | |
} | |
URLSession.shared.dataTask(with: imageURL) { data, response, error in | |
guard let data = data, error == nil else { | |
DispatchQueue.main.async { | |
self.didChange.send(nil) | |
} | |
return | |
} | |
self.downloadedImage = UIImage(data: data) | |
DispatchQueue.main.async { | |
self.didChange.send(self) | |
} | |
}.resume() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment