被裁剪实际上不会在SwiftUI中裁剪图像

时间:2020-08-07 10:57:45

标签: swiftui imageview xcode11 sdwebimage clipped

我正在尝试裁剪图像,正如我们所见,UI看起来不错,但实际上并没有裁剪图像,这会导致其他UI元素无响应。

enter image description here

这是我正在使用的代码。

struct ImageContentView: View {
    var urls:[String] = [
        "https://lh3.googleusercontent.com/proxy/80im-IBfLODpLDj8d02uEpSVIhqdjen6H6CeFwgRBxeua-Dgw0R3WONFj1Gk8CwB_MufmC9rQ8qHjyWMejwFcJ1PA2s8AAu5WVsmJA=s0-d",
        "https://wallpaperaccess.com/full/530919.jpg"
    ] 
    var body: some View {
        ScrollView{
            VStack{
                Button(action: {
                    
                }, label: {
                    Text("Hello")
                })
                VStack(spacing: 20.0) {
                    ForEach(self.urls, id:\.self) { url in
                        WebImage(url: URL.init(string: url)!)
                            .resizable()
                            .aspectRatio(contentMode: ContentMode.fill)
                            .frame(height: UIScreen.main.bounds.size.width * 0.5) 
                            .clipped()
                            .cornerRadius(10.0)
                            .shadow(color: Color.red, radius: 10.0, x: 0, y: 0)
                    }
                }.padding()
            }
        }
    }
}    

1 个答案:

答案 0 :(得分:0)

这是固定的部分(已通过Xcode 12 / iOS 14测试)

VStack(spacing: 20.0) {
    ForEach(self.urls, id:\.self) { url in
        WebImage(url: URL.init(string: url)!)
            .resizable()
            .aspectRatio(contentMode: ContentMode.fill)
            .frame(height: UIScreen.main.bounds.size.width * 0.5)
            .clipped()
            .cornerRadius(10.0)
            .shadow(color: Color.red, radius: 10.0, x: 0, y: 0)
    }.contentShape(Rectangle())   // << here !!
}.padding()

注意:我不知道您的WebImage是什么,但也复制了Image和本地图像,因此已测试了修复。