以下屏幕上的SwiftUI挂起时间为几秒钟.
这段代码是复制粘贴的,它构成了我的简单应用程序的一个标签.第一次显示此选项卡时出现一次挂起.
图像数量约为150;图像大小为1920 x1080.
struct PhotosView : View
{
@FetchRequest(sortDescriptors: [SortDescriptor(\Photo.date, order: .reverse)], animation: .default)
private var photos: FetchedResults<Photo>
@State private var gridHeight = 180.0
var body: some View
{
VStack
{
ScrollView(.horizontal)
{
LazyHGrid(rows: [GridItem(.fixed(gridHeight))], alignment: .top, spacing: 0)
{
ForEach(photos)
{ photo in
if let data = photo.filteredData
{
Image(data: data)
.resizable()
.scaledToFit()
}
}
}
.frame(height: 200)
}
}
}
}
我以为,使用LazyHGrid
最初只会加载一小部分(目前屏幕上只有一个半大小).
我如何才能提高性能?