gpt4 book ai didi

ios - 如何制作方形图像的 LazyVGrid?

转载 作者:行者123 更新时间:2023-12-05 08:16:54 25 4
gpt4 key购买 nike

如何制作类似于照片库的方形图像?

我正在尝试以下操作:

let columns: [GridItem] = [
GridItem(.flexible()),
GridItem(.flexible())
]

LazyVGrid(
columns: columns,
alignment: .center
) {
ForEach(self.selectedImages, id: \.self) { image in
Image(uiImage: image)
.resizable()
.scaledToFit()
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.aspectRatio(1, contentMode: .fill)
.background(Color.blue)
}
}

但是,这会导致:

enter image description here

我想构建一个 2x2 正方形网格(类似于下面的这个 3x3 - 我希望图像很好地适合正方形)

enter image description here

最佳答案

试试这个。它提供正确的宽高比并正确地裁剪图像以填充正方形:

ForEach(self.selectedImages, id: \.self) { image in
Color.blue
.aspectRatio(1, contentMode: .fill)
.overlay(
Image(uiImage: image)
.resizable()
.scaledToFill()
).clipped()
}

关于ios - 如何制作方形图像的 LazyVGrid?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64420313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com