SwiftUI URLImage 水平滚动视图

SwiftUI URLImage Horizontal ScrollView

所以我一直在阅读 SwiftUI instagram 教程并学习了如何在标准 3x3 instagram 视图中将用户上传的图像加载到 firebase,但现在我想扩展我的知识并在水平滚动视图中练习。

这是我必须创建网格视图的内容:

    import SwiftUI
    import URLImage
    import FirebaseAuth
    
    struct Photo: Identifiable {
        let id = UUID()
        var photo = ""
    }
    
    struct PhotoView: View {
        @Environment(\.presentationMode) var mode: Binding<PresentationMode>
        @EnvironmentObject var session: SessionStore
        @ObservedObject var profileViewModel = ProfileViewModel()

var body: some View {
        
        return
                ScrollView {
                    if !profileViewModel.isLoading {
                        VStack(alignment: .leading, spacing: 1) {
                            // rows
                            ForEach(0..<self.profileViewModel.splitted.count) { index in
                                HStack(spacing: 1) {
                                    // Columns
                                    ForEach(self.profileViewModel.splitted[index], id: \.postId) { post in
                                        
                                        URLImage(URL(string: post.mediaUrl)!,
                                        content: {
                                            [=10=].image
                                                .resizable()
                                                .aspectRatio(contentMode: .fill)
                                            }).frame(width: UIScreen.main.bounds.width / 3, height: UIScreen.main.bounds.width / 3).clipped().cornerRadius(5)
                                        
                                    }
                                }
                            }
                        }.frame(width: UIScreen.main.bounds.width, alignment: .leading).padding(.top, 2)
                    }
 
                }.navigationBarTitle(Text("Photos"), displayMode: .inline).navigationBarBackButtonHidden(true).navigationBarItems(leading: Button(action : {
                    self.mode.wrappedValue.dismiss()
                }) {
                    Image(systemName: "arrow.left")
                }).onAppear {
                    self.profileViewModel.loadUserPosts(userId: Auth.auth().currentUser!.uid)
        }
    }
}

extension Array {
    
    func splitted(into size:Int) -> [[Element]] {
        var splittedArray = [[Element]]()
        if self.count >= size {
            for index in 0...self.count {
                if index % size == 0 && index != 0 {
                    splittedArray.append(Array(self[(index - size)..<index]))
                } else if (index == self.count) {
                    splittedArray.append(Array(self[index - 1..<index]))
                }
            }
            
        } else {
            splittedArray.append(Array(self[0..<self.count]))
        }
        return splittedArray
    }
}

class ProfileViewModel: ObservableObject {
    @Published var posts: [Post] = []
    @Published var isLoading = false
    var splitted: [[Post]] = []
    
    func loadUserPosts(userId: String) {
        isLoading = true
        Api.User.loadPosts(userId: userId) { (posts) in
            self.isLoading = false
            self.posts = posts
            self.splitted = self.posts.splitted(into: 3)
        }
    }
}

这是它的样子:

这是我想要实现的示例代码:

import SwiftUI
import URLImage
import FirebaseAuth

struct TestView: View {
    @Environment(\.presentationMode) var mode: Binding<PresentationMode>
    var body: some View {
        VStack {
            
            ScrollView(.horizontal, showsIndicators: false) {
                HStack(spacing: 2) {
                    ForEach(1..<5) { _ in
                        Image("photo3").resizable()
                            .clipShape(Rectangle())
                            .aspectRatio(contentMode: ContentMode.fill)
                            .frame(width: 100, height: 100).cornerRadius(10).opacity(1).shadow(radius: 4)
                    }
                    
                }
            }.navigationBarTitle(Text("Photos"), displayMode: .inline).navigationBarBackButtonHidden(true).navigationBarItems(leading: Button(action : {
                self.mode.wrappedValue.dismiss()
            }) {
                Image(systemName: "arrow.left")
            })
            Spacer()
        }.padding()
    }
}

这里是我想要的示例图像:

我真的很难理解 ForLoop 部分以及如何将图像检索到简单的 scrollView 中。

如有任何帮助,我们将不胜感激!

谢谢!

您想遍历模型中的帖子。借用您早期的代码,您需要这样的东西:

ScrollView(.horizontal, showsIndicators: false) {
    HStack(spacing: 2) {

        ForEach(self.profileViewModel.posts, id: \.postId) { post in
                                        
            URLImage(URL(string: post.mediaUrl)!,
                content: {
                    [=10=].image
                    .resizable()
                    .aspectRatio(contentMode: .fill)
                }
            )
            .frame(width: 100, height: 100)
            .clipped()
            .cornerRadius(10)
            .shadow(radius: 4)
        }
    }
}

vacawama 已经发布了完美的解决方案,使其看起来像您的示例。

只需添加您获得结果的原因即可。

您的代码与示例代码之间的区别在于您使用了两个 ForEach,一个用于行,一个用于列。该数组与您的扩展分开,因此您得到行和列。

//Rows
ForEach(0..<self.profileViewModel.splitted.count) { index in
    HStack(spacing: 1) {
        // Columns
        ForEach(self.profileViewModel.splitted[index], id: \.postId) { post in

您的评论已经说明了它是如何工作的。如果您想将所有图像都放在水平滚动条中,您只需要一个 ForEach 即可将所有图像输出到 ScrollView 中。

ScrollView(.horizontal, showsIndicators: false) {
    HStack(spacing: 2) {
        ForEach(self.profileViewModel.posts, id: \.postId) { post in