如何将 AsyncImage 对象的返回图像传递到 SwiftUI 中的不同视图?
我需要将 AsyncImage 的图像传递到作为 NavigationLink 的目的地的另一个 SwiftUI 视图。
我目前正在为列表单元格使用一个 AsyncImage,为详细信息屏幕使用另一个 AsyncImage,但这当然会导致同一张图像异步加载两次,我想点击列表单元格项目并使用已加载的图像推送到详细信息视图所以我不必再次加载它。
我目前在 CellView 上做什么:
AsyncImage(url: URL(string: movieStringUrl ?? "")) { image in
image.resizable()
.aspectRatio(contentMode: .fill)
} placeholder: {
ProgressView()
}
在 ContentView 上:
ForEach(viewModel.movies) { movie in
NavigationLink(destination: MovieDetailView(movie: movie, viewModel: viewModel)) {
CellView(movie: movie, viewModel: viewModel)
}
}
在详细视图上:
AsyncImage(url: URL(string: movieStringUrl ?? "")) { image in
image
.resizable()
.aspectRatio(contentMode: .fill)
} placeholder: {
ProgressView()
}
我希望能够将常规图像对象(不是 AsyncImage)传递到详细视图,这样我就可以避免加载相同的图像两次,但由于图像是在结束时,我不太确定如何实现这一点。谢谢!
I need to pass the Image of an AsyncImage to another SwiftUI View that is the destination of a NavigationLink.
I am currently using an AsyncImage for the List cell and another one for the detail screen, but this of course causes the same image to load asynchronously twice, Id like to tap the List cell item and push to the Detail View with the already loaded image so I don't have to load it again.
What Im currently doing on CellView:
AsyncImage(url: URL(string: movieStringUrl ?? "")) { image in
image.resizable()
.aspectRatio(contentMode: .fill)
} placeholder: {
ProgressView()
}
On ContentView:
ForEach(viewModel.movies) { movie in
NavigationLink(destination: MovieDetailView(movie: movie, viewModel: viewModel)) {
CellView(movie: movie, viewModel: viewModel)
}
}
On detail view:
AsyncImage(url: URL(string: movieStringUrl ?? "")) { image in
image
.resizable()
.aspectRatio(contentMode: .fill)
} placeholder: {
ProgressView()
}
Id like to be able to pass the regular Image object (not AsyncImage) to the detail view so I can avoid having to load the same image twice, but since the Image is in a closure, Im not quite sure how to accomplish this. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AsyncImage 使用共享的 URLSession,因此您可以在您的
URLSession
上设置URLCache
然后它不会再次下载,只需从缓存中读取。它可以是内存或磁盘缓存。编辑:有一个默认缓存,因此您可能不需要执行任何操作,它就会正常工作。
AsyncImage uses the shared URLSession so sou could set a
URLCache
on yourURLSession
then it won't be downloaded again, just read from the cache. It can either be a memory or disk cache.Edit: there is a default cache so you probably don’t have to do anything and it’ll just work.