Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 16 additions & 20 deletions firebaseai/ImagenScreen/ImagenScreen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -44,28 +44,24 @@ 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(.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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using id: \.self with UIImage can cause performance issues because UIImage is a class. When images are regenerated, new UIImage instances cause SwiftUI to re-render the entire grid. Consider wrapping UIImage in a custom struct that conforms to Identifiable (e.g., using a UUID for the id) to improve UI performance.

Copy link
Contributor Author

@YoungHypo YoungHypo Jun 26, 2025

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

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()
}
.padding(.horizontal, spacing)
}
.padding(.horizontal, spacing)
}
if viewModel.inProgress {
ProgressOverlay()
}
.overlay(viewModel.inProgress ? ProgressOverlay() : nil)
}
.navigationTitle("Imagen example")
.onAppear {
Expand Down