【发布时间】:2022-12-12 22:25:07
【问题描述】:
为了用大量数据填充 ScrollView,苹果建议在 ScrollView 中使用 LazyVGrid:LazyVGrid - Apple
因此,这个加载 500 张随机照片的示例代码在内存管理的情况下完美运行:(将近 30MB)。
我使用 KingFisher 作为图像加载器。
var body: some View {
ScrollView {
LazyVGrid(columns: [GridItem(.flexible())]) {
ForEach(1...500, id: \.self) { value in
KFImage(URL(string: "https://picsum.photos/800/800?\(value)"))
}
}
}
}
但是我想控制ScrollView的内容,比如offset。
所以一旦我添加.content.offset(y: 5),内存就会上升640MB!!
var body: some View {
ScrollView {
LazyVGrid(columns: [GridItem(.flexible())]) {
ForEach(1...500, id: \.self) { value in
KFImage(URL(string: "https://picsum.photos/800/800?\(value)"))
}
}
}
.content.offset(y: 5)
}
.content背后有什么原因吗?!!我该如何处理没有大量内存?
我需要手动禁用滚动和控制内容偏移,而不是使用ScrollViewReader。
谢谢
【问题讨论】:
标签: swift swiftui scrollview offset lazyvgrid