swiftui - Clipped 并不是在 SwiftUI 中实际剪辑图像

标签 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()
            }
        }
    }
}    

最佳答案

这是固定部分(使用 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并且它也被复制了本地镜像,因此对修复进行了测试。

关于swiftui - Clipped 并不是在 SwiftUI 中实际剪辑图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63300411/

相关文章:

swift - 如何捕获通过 SwiftUI 中的函数更改的值更改?

node.js - 使用 Swift 的 Socket.io 不是握手

android - 在 ImageView 中返回 BitmapDrawable

ios - iOS 11 和 Xcode 11 beta 上的命名颜色问题

swiftui - SwiftUI 2.0 中的 navigationBarTitle 和 navigationTitle 有什么区别?

java - 在字符串中显示带有图像位置的图像

java - 获取点击图像属性

ios - NSDATA 描述返回 nil

swift - 如何在 Swift 中重复链接动画?

ios - 使用 SwiftUI 接收 APNS 后显示 View