-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Refactor the UI layout of ImagenScreen to solve the navigation flicker #1719
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
f60ea50
0a3b325
27f2c16
e105b4d
4e96db2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,7 +33,7 @@ struct ImagenScreen: View { | |
var focusedField: FocusedField? | ||
|
||
var body: some View { | ||
ZStack { | ||
ScrollView { | ||
VStack { | ||
InputField("Enter a prompt to generate an image", text: $viewModel.userInput) { | ||
Image( | ||
|
@@ -44,28 +44,22 @@ struct ImagenScreen: View { | |
.focused($focusedField, equals: .message) | ||
.onSubmit { sendOrStop() } | ||
|
||
ScrollView { | ||
let spacing: CGFloat = 10 | ||
LazyVGrid(columns: [ | ||
GridItem(.fixed(UIScreen.main.bounds.width / 2 - spacing), spacing: spacing), | ||
GridItem(.fixed(UIScreen.main.bounds.width / 2 - spacing), spacing: spacing), | ||
], spacing: spacing) { | ||
ForEach(viewModel.images, id: \.self) { image in | ||
Image(uiImage: image) | ||
.resizable() | ||
.aspectRatio(contentMode: .fill) | ||
.frame(width: UIScreen.main.bounds.width / 2 - spacing, | ||
height: UIScreen.main.bounds.width / 2 - spacing) | ||
.cornerRadius(12) | ||
.clipped() | ||
} | ||
let spacing: CGFloat = 10 | ||
LazyVGrid(columns: [ | ||
GridItem(.flexible(), spacing: spacing), | ||
GridItem(.flexible(), spacing: spacing), | ||
], spacing: spacing) { | ||
ForEach(viewModel.images, id: \.self) { image in | ||
Image(uiImage: image) | ||
.resizable() | ||
.aspectRatio(1, contentMode: .fill) | ||
.cornerRadius(12) | ||
.clipped() | ||
} | ||
.padding(.horizontal, spacing) | ||
} | ||
.padding(.horizontal, spacing) | ||
} | ||
if viewModel.inProgress { | ||
ProgressOverlay() | ||
} | ||
.overlay(viewModel.inProgress ? ProgressOverlay() : nil) | ||
|
||
} | ||
.navigationTitle("Imagen example") | ||
.onAppear { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
id: \.self
withUIImage
can cause performance issues becauseUIImage
is a class. When images are regenerated, newUIImage
instances cause SwiftUI to re-render the entire grid. Consider wrappingUIImage
in a customstruct
that conforms toIdentifiable
(e.g., using aUUID
for theid
) to improve UI performance.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’ve tried to generate images multiple times in
imagenscreen
. The UI remained smooth without noticeable lag when re-rendering the image grid. If you think adjustments are necessary, I'll do that. @paulb777