在 SwiftUI 中仅显示图像的顶部

Show only top portion of image in SwiftUI

我需要以仅显示图像顶部的方式对齐 UIImageView。

原图:-

我从下面的代码中获得了图像:-

代码:-

Image("ImageDemo")
  .resizable()
  .scaledToFill()
  .frame(height: 120, alignment: .center)
  .clipped()
  .cornerRadius(20, corners: [.topLeft, .topRight])
  .padding(.bottom)

谁能告诉我如何只显示顶部的图像,我已经尝试通过上面实现但还没有结果。

如有任何帮助,我们将不胜感激。

提前致谢。

使用 alignment: .top 它会解决您的问题。


struct ContentView: View {
    var body: some View {

            Image("Your Image Here!")
                .resizable()
                .scaledToFill()
                .cornerRadius(20)
                .frame(height: 120, alignment: .top) //  <<: Here
                .clipped()
                .padding()

    }
}