【问题标题】:Clipped not actually clips the Image in SwiftUIClipped 实际上并不在 SwiftUI 中剪辑图像
【发布时间】:2020-08-07 10:57:45
【问题描述】:

我正在尝试剪辑图像,当我们看到 UI 时,它看起来不错,但实际上它不会剪辑导致其他 UI 元素无响应的图像。

这是我正在使用的代码。

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

【问题讨论】:

  • 我尝试了 ContentShape 和 ClipShape,但没有成功。
  • 这个有什么更新吗?你找到解决办法了吗?

标签: swiftui imageview xcode11 sdwebimage clipped


【解决方案1】:

这里是固定部分(使用 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 和本地图像也被复制了,因此测试了修复。

【讨论】:

  • WebImage 来自 SDWebImageSwiftUI 库
  • 这对我不起作用。我正在从 URL 下载图像并显示它
  • 那么它也是WebImage中的错误,因为上面适用于Image。您也可以尝试将 .contentShape 放在 VStack 上。
  • 谢谢!只需添加 contentShape Rectangle 修饰符就可以解决我的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-08-04
  • 1970-01-01
  • 1970-01-01
  • 2021-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多