我使用 LazyVGrid 来显示文章库。一切正常,我没有内存警告,因为每次视图离开屏幕时,内存使用量都会减少。
我的问题是当我对图像使用matchedGeometryEffect时,以便将动画与新视图同步。
动画效果很好,但是在滚动视图滚动时内存正在增加。就像matchedGeometryEffect正在维护对对象的内存引用,并且不允许释放。
容器
LazyVGrid(
columns: [
GridItem(.flexible())
],spacing: 16
){
ForEach(viewModel.articles){ article in
LazyVStack{
ArticleCardView(article: article, animation: animation, show: $show)
.onTapGesture {
withAnimation(.spring()){
selectedArticle = article
show.toggle()
}
}
}
}
}
卡片视图
VStack{
if !show {
Image(uiImage: readImage(name: "\(article.id)00"))
.resizable()
.aspectRatio(contentMode: .fit)
//.matchedGeometryEffect(id: "img\(article.id)00", in: animation)
...
新观点
VStack{
GeometryReader { geo in
TabView {
Image(uiImage: readImage(name: "\(article!.id)00"))
.resizable()
.aspectRatio(contentMode: .fit)
//.matchedGeometryEffect(id: "img\(article!.id)00", in: animation, isSource: false)
.tag(1)
...
一切都很完美,但是如果我取消注释注释行,滚动时内存使用量会增加。
任何想法?比你