Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import SwiftUI
- import Combine
- import AlamofireImage
- let imageDownloader = ImageDownloader(
- configuration: ImageDownloader.defaultURLSessionConfiguration(),
- downloadPrioritization: .fifo,
- maximumActiveDownloads: 4,
- imageCache: AutoPurgingImageCache()
- )
- extension ImageDownloader {
- func image(for url: URL) -> AnyPublisher<UIImage?, Never> {
- return Publishers.Future { subscriber in
- let urlRequest = URLRequest(url: url)
- imageDownloader.download(urlRequest) { response in
- if let image = response.result.value {
- subscriber(.success(image))
- }
- }
- }
- .eraseToAnyPublisher()
- }
- }
- struct RemoteImage: View {
- // let placeholder = UIImage(named: "placeholder.jpg")!
- let url: URL
- init(url: URL) {
- print("RemoteImage created \(url)")
- self.url = url
- }
- @State private var image: UIImage? = nil
- var body: some View {
- Group {
- if image != nil {
- Image(uiImage: image!)
- } else {
- Image(systemName: "person.crop.circle")
- }
- }
- .bind(imageDownloader.image(for: url), to: $image)
- }
- }
- //#if DEBUG
- //struct RemoteImage_Previews : PreviewProvider {
- // static var previews: some View {
- // RemoteImage()
- // }
- //}
- //#endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement