在 SwiftUI 中仅显示图像的顶部部分

Sam*_*Sam 0 image uiimageview swift swiftui

我需要以仅显示图像顶部的方式对齐 a\xc2\xa0UIImageView\xc2\xa0 。

\n

原始图片:-

\n

截屏

\n

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

\n

截屏

\n

代码:-

\n
Image("ImageDemo")\n  .resizable()\n  .scaledToFill()\n  .frame(height: 120, alignment: .center)\n  .clipped()\n  .cornerRadius(20, corners: [.topLeft, .topRight])\n  .padding(.bottom)\n
Run Code Online (Sandbox Code Playgroud)\n

有人可以向我解释如何仅显示顶部部分的图像吗?我已经尝试通过上面的方法实现,但还没有结果。

\n

任何帮助将不胜感激。

\n

提前致谢。

\n

use*_*ser 5

使用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()

    }
}
Run Code Online (Sandbox Code Playgroud)