M. *_*Hig 3 image crop uiimage swiftui geometryreader
我想使用 SwiftUI 将其分成中心的两部分。给我留下了两张可以访问的独立图像。但是我不知道如何将原件分成顶部和底部。这两块必须对齐才能创建原始图像。
我尝试使用几何读取器读取高度和宽度并返回高度一半的图像,但是这两个图像不喜欢这样。
GeometryReader { geo in
image
.frame(width: geo.width, height: geo.height / 2, alignment: .center)
.clipped()
}
Run Code Online (Sandbox Code Playgroud)
这是执行此操作的一种方法:使用 usingclipped()修饰符。
struct ContentView: View {
@State private var spacing: CGFloat = CGFloat()
@State private var imageSize: CGSize = CGSize()
var body: some View {
let image = Image(systemName: "star")
.resizable()
.scaledToFit()
.frame(width: 200, height: 200, alignment: .center)
.background(Color.yellow)
.cornerRadius(10.0)
.background(GeometryReader { proxy in Color.clear.onAppear() { imageSize = proxy.size } })
return ZStack {
VStack(spacing: spacing) {
image.frame(width: imageSize.width, height: imageSize.height/2, alignment: .top).clipped()
image.frame(width: imageSize.width, height: imageSize.height/2, alignment: .bottom).clipped()
}
VStack { Spacer(); Slider(value: $spacing,in: 0.0...100.0) }
}
.padding()
.compositingGroup()
.shadow(radius: 10.0)
}
}
Run Code Online (Sandbox Code Playgroud)