AsyncImage inside list在滚动时消耗大量内存,最终在SwiftUI中崩溃

pinkon5k  于 2023-08-02  发布在  Swift
关注(0)|答案(1)|浏览(96)

以下是我所做的:当内存超过1 GB或更大时,应用程序会崩溃。我试着注解asyncImage,它只占用了25-30 MB的内存,而且没有崩溃。

List {
            ForEach(0..<(self.imageURLArray.count), id: \.self) { i in
                AsyncImage(url: URL(string: self.imageURLArray[i])) { image in
                    image.resizable()
                        .aspectRatio(16/9, contentMode: .fit)
                        .cornerRadius(10, antialiased: true)
                        .padding(.horizontal)
                        .padding(.vertical, 5)
                }
                placeholder: {
                    ZStack {
                        RoundedRectangle(cornerRadius: 10)
                            .foregroundColor(.black.opacity(0.1))
                            .aspectRatio(16/9, contentMode: .fit)
                            .padding(.horizontal)
                            .padding(.vertical, 10)
                        ProgressView()
                            .progressViewStyle(.circular)
                    }
                }
                .onAppear {
                    if self.imageURLArray[i] == self.imageURLArray.last {
                        self.loadMoreItems()
                    }
                }
            }
            .listRowInsets(EdgeInsets())
        }

字符串

uajslkp6

uajslkp61#

我只是把asyncImage放在LazyVStack中,现在它只为可见的图像消耗内存。

List {
        ForEach(0..<(self.imageURLArray.count), id: \.self) { i in
            LazyVStack {
                AsyncImage(url: URL(string: self.imageURLArray[i])) { image in
                    image.resizable()
                        .aspectRatio(16/9, contentMode: .fit)
                        .cornerRadius(10, antialiased: true)
                        .padding(.horizontal)
                        .padding(.vertical, 5)
                }
                placeholder: {
                    ZStack {
                        RoundedRectangle(cornerRadius: 10)
                            .foregroundColor(.black.opacity(0.1))
                            .aspectRatio(16/9, contentMode: .fit)
                            .padding(.horizontal)
                            .padding(.vertical, 10)
                        ProgressView()
                            .progressViewStyle(.circular)
                    }
                }
                .onAppear {
                    if self.imageURLArray[i] == self.imageURLArray.last {
                        self.loadMoreItems()
                    }
                }
            }
        }
        .listRowInsets(EdgeInsets())
    }

字符串

相关问题