简体   繁体   English

如何在 SwiftUI 的范围内正确填充填充图像?

[英]How to properly fit a filled Image in its bounds on SwiftUI?

I have to fix a problem with Images on SwiftUI. I currently have to fill the images I receive in a certain vertical container, which are the cells of a LazyVGrid, but the problem is that the tappable area expands over the designated one.我必须解决 SwiftUI 上的图像问题。我目前必须在某个垂直容器中填充我收到的图像,这些容器是 LazyVGrid 的单元格,但问题是可点击区域扩展到指定区域。

I used the Accessibility Inspector, and I found out that the area in excess is due the Images.我使用了 Accessibility Inspector,我发现多余的区域是由于图像。 (screenshot attached) (附截图)

The code I'm using is something similar to the following:我正在使用的代码类似于以下内容:

Image(uiImage: image.image)
    .resizable()
    .aspectRatio(contentMode: .fill)
    .frame(
        maxWidth: width,
        maxHeight: height
    )
    .clipped()

I thought that by having the frames and the clipped method I could get rid of this problem, but that's not the case unless I'm using some squared images.我认为通过使用框架和裁剪方法我可以摆脱这个问题,但事实并非如此,除非我使用一些方形图像。 Any idea of what could work to fix this problem?知道什么可以解决这个问题吗? Thanks!谢谢!

在此处输入图像描述

Wrap content in Geometry Reader and you need to know the aspect ratio like I set 720/460 below在 Geometry Reader 中包裹内容,你需要知道宽高比,就像我在下面设置的 720/460

GeometryReader { gr in
       Image(uiImage: image.image)
            .resizable()
             .aspectRatio(720/460, contentMode: .fit)
          }
        .clipped()
        .aspectRatio(720/460, contentMode: .fit)

Just add .contentShape(Rectangle()) at the bottom:只需在底部添加.contentShape(Rectangle())

            Image(uiImage: image.image)
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(
                    maxWidth: width,
                    maxHeight: height
                )
                .clipped()
                .contentShape(Rectangle())    // <- Here!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM