Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Rough sketch of SwiftUI RemoteImage using AlamofireImage
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) { response in
if let image = response.result.value {
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: "")
.bind(imageDownloader.image(for: url), to: $image)
//#if DEBUG
//struct RemoteImage_Previews : PreviewProvider {
// static var previews: some View {
// RemoteImage()
// }

This comment has been minimized.

Copy link
Owner Author

alexito4 commented Jun 8, 2019


This comment has been minimized.

Copy link

patchthecode commented Jun 13, 2019

fails to build on this line

.bind(imageDownloader.image(for: url), to: $image)


Protocol type 'Any' cannot conform to 'View' because only concrete types can conform to protocols


This comment has been minimized.

Copy link

sergdort commented Jun 16, 2019

bind is a convenience extension that I've created. The complete implementation can be found here

extension View {
    func bind<P: Publisher, Value>(
        _ publisher: P,
        to state: Binding<Value>
    ) -> SubscriptionView<P, Self> where P.Failure == Never, P.Output == Value {
        return onReceive(publisher) { value in
            state.value = value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.