paololeonardi / WaterfallGrid

A waterfall grid layout view for SwiftUI.
MIT License
2.39k stars 117 forks source link

Getting Cannot convert value of type 'some View' to closure result type '_' #46

Closed ethanyuwang closed 3 years ago

ethanyuwang commented 4 years ago
struct WaterfallGallery: View {
    @ObservedObject var loader: ImagesLoader
    @State var urlPaths: [String]

    init(urlPaths: [String]) {
        var URLs = [URL]()
        for urlPath in urlPaths {
            //TODO: provide defualt error image
            URLs.append(URL(string: urlPath)!)
        }
        self._urlPaths = State(initialValue: urlPaths)
        self.loader = ImagesLoader(urls: URLs)
    }

    var body: some View {
        Group {
            if self.loader.isLoading {
                placeHolder
            } else {
                gallery
            }
        }
            .onAppear(perform: loader.load)
            .onDisappear(perform: loader.cancel)
    }

    private var gallery: some View {
            WaterfallGrid(self.loader.$images) { uiImage in
                Image(uiImage: uiImage).resizable().aspectRatio(contentMode: .fit).cornerRadius(10)
            }
           .gridStyle(columns: 2)
    }

    private var placeHolder: some View {
         Text("Loading")
    }
}

I followed the example to use WaterfallGrid but keep getting Cannot convert value of type 'some View' to closure result type '_' on line:

Image(uiImage: uiImage).resizable().aspectRatio(contentMode: .fit).cornerRadius(10)

kennylugo commented 4 years ago

Hey! Any luck on a solution?

paololeonardi commented 4 years ago

Hi, I believe the issue here is that you are trying to initialize the grid with a type Published<[UIImage]>.Publisher and the generic parameter 'ID' of the collection could not be inferred.

try:

private var gallery: some View {
  WaterfallGrid(self.loader.images, id:\.self) { uiImage in
    Image(uiImage: uiImage).resizable().aspectRatio(contentMode: .fit).cornerRadius(10)
  }
  .gridStyle(columns: 2)
}
andy-zhangtao commented 4 years ago

Does uiImage class extends Identifiable, Hashable ?