简体   繁体   English

为什么图像被切断?

[英]Why is are the images being cut off?

The images in my 'tiles' are being cut off at the sides.我的“瓷砖”中的图像在两侧被切断。 I am trying to create a tile for each 'product' that displays an image, name and subtitle.我正在尝试为每个显示图像、名称和副标题的“产品”创建一个磁贴。 Everything now works as it should besides the image.除了图像之外,现在一切正常。 This is because the images on the tiles are being cut off at the sides.这是因为图块上的图像在两侧被切掉了。

The view for the ContentView is as follows: ContentView 的视图如下所示:

     ScrollView(.vertical, showsIndicators: false, content: {
                    
                    Spacer()
                    LazyVGrid(columns: Array(repeating: GridItem(.flexible(),spacing: 15), count: 2),spacing: 10){
                        
                        ForEach(HomeModel.filteredProduct){product in
                            
                            // Product View...
                            
                                
                                ProductView(productData: product)
                                .background(.white)
                                .clipShape(RoundedRectangle(cornerRadius: 15, style: .continuous))
                                .shadow(color: .black.opacity(0.4), radius: 3, x: 1, y: 1)
                                    .onTapGesture {
                                        
                                        withAnimation(.easeIn){
                                            
                                            selectedProduct = charity
                                            show.toggle()
                                        }
                                    }
                        }
                    }
                    Spacer()
                    .padding()
                    .padding(.top,10)
                })
                .padding(.top, 20)

And the ProductView code is as follows: ProductView代码如下:

    var body: some View {
    
        
            VStack(alignment: .center, spacing: 0) {
                
                WebImage(url: URL(string: productData.product_image))
                    .resizable()
                    .aspectRatio(contentMode: .fill)
                    .frame(width: 160, height: 225)
                    .cornerRadius(15)
                    .clipped()
                    .padding(5)
               
            Text(productData.product_name)
                .font(.title2)
                .fontWeight(.bold)
                .foregroundColor(.black)
                .padding()
            
                
            Text(productData.product_type)
                .font(.caption)
                .foregroundColor(.gray)
                .lineLimit(2)
                .padding()
        }
}

Here is an image showing what is meant by 'the tile is cutting it off':这是一张图片,显示了“瓷砖正在切断”的含义:

平铺图像

As you can see in the photo, the image is not all included because it doesn't fit in the tile.正如您在照片中看到的那样,图像并未全部包含在内,因为它不适合磁贴。 This needs to be resized.这需要调整大小。

The first issue here would be the .contentMode modifier it needs to be set to fill.这里的第一个问题是需要设置为填充的.contentMode修饰符。 If you have an image where the width is larger then the height but you set the frame at a different aspect ratio you will see a white space above and below the image.如果您的图像宽度大于高度,但您将框架设置为不同的纵横比,您将在图像上方和下方看到一个空白区域。

But there were other problems too.但也有其他问题。 You need to clip an image with the .clipped() modifier after you set its frame, else it will overflow the frame.您需要在设置框架后使用.clipped()修饰符裁剪图像,否则它会溢出框架。 Why all those ZStacks ?为什么所有这些ZStacks I cannot see any purpose of adding a ZStack with only one child.我看不出添加只有一个孩子的ZStack的任何目的。


BUT:但:

As your code is not reproducible i had to add or remove several things to make it work.由于您的代码不可重现,我必须添加或删除一些内容才能使其正常工作。 So you need to adopt this to your code.所以你需要在你的代码中采用它。


Solution:解决方案:

struct ContentView: View{
    
    @State private var products: [ProductData] = [ProductData(product_name: "test", product_details: "test"), ProductData(product_name: "test2", product_details: "test2"), ProductData(product_name: "test3", product_details: "test3")]
    @State private var show = false
    @State private var selectedProduct: ProductData?
    
    var body: some View{
        ScrollView(.vertical, showsIndicators: false, content: {
            Spacer()
            LazyVGrid(columns: Array(repeating: GridItem(.flexible(),spacing: 15), count: 2),spacing: 10){
                
                ForEach(products){product in
                    // Product View...
                    ProductView(productData: product)
                        .background(.white)
                        .clipShape(RoundedRectangle(cornerRadius: 15, style: .continuous))
                        .shadow(color: .black.opacity(0.4), radius: 3, x: 1, y: 1)
                        .onTapGesture {
                            withAnimation(.easeIn){
                                selectedProduct = product
                                show.toggle()
                            }
                        }
                }
            }
            Spacer()
                .padding()
                .padding(.top,10)
        })
        .padding(.top, 20)
    }
}

struct ProductData: Identifiable{
    var id = UUID()
    var product_name: String
    var product_details: String
}


struct ProductView: View{
    var productData: ProductData
    
    var body: some View{
        VStack(alignment: .center, spacing: 0) {
            Image("test")
                .resizable()
                .aspectRatio(contentMode: .fill)
                .frame(width: 160, height: 225)
                .cornerRadius(15)
                .clipped()
                .padding(5)
            
            Text(productData.product_name)
                .font(.title2)
                .fontWeight(.bold)
                .foregroundColor(.black)
                .padding()
            
            Text(productData.product_details)
                .font(.caption)
                .foregroundColor(.gray)
                .lineLimit(2)
                .padding()
        }
        
    }
}

Outcome:结果:

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM