【问题标题】:How to make a LazyVGrid of square images?如何制作方形图像的 LazyVGrid?
【发布时间】:2020-10-19 16:04:21
【问题描述】:

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

我正在尝试以下方法:

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)
    }
}

但是,这会导致:

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

【问题讨论】:

  • 这是否回答了您的问题stackoverflow.com/a/63027052/12299030
  • 实际上这对我不起作用:(如果我使用geometryReader设置宽度和高度,它会起作用,但这似乎有点对吗?

标签: ios swift swiftui


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    • 1970-01-01
    • 2017-01-19
    • 1970-01-01
    相关资源
    最近更新 更多