我面临着一些小问题,调整自定义图像的大小,以完全填充列表行. screenshot

具有zoom 效果的图像: screenshot2

在截图中,可以看到它正在被剪裁.我发现我可以作弊的唯一方法是使用.scaleEffect(1.13),这并不理想,因为我必须找到并调整数字来填补空格.

我try 了很多其他我在网上找到的 Select ,但都没有成功.所有注释的代码行都不起作用.

.background(
    displayImageView(for: post)
    //.scaleEffect(1.13)
    //.aspectRatio(contentMode: .fill)
    //.frame(minWidth: 0, maxWidth: .infinity)
    //.scaledToFill()
    //.scaledToFit() 
    .opacity(0.4)
            )
            //.cornerRadius(10)
            //.clipped()
        }
    }

自动填充它的正确方式是什么?

代码:

struct PostView: View {
    var post: test2
    
    var body: some View {
        VStack(alignment: .leading, spacing: 4) {
            HStack(alignment: .top) {
                // Display Image (100x100)
                displayImageView(for: post)
                    .frame(width: 100, height: 100)
                    .cornerRadius(10)
                    .padding(.leading, -8)

                // Profile Picture, Username, and Caption
                VStack(alignment: .leading, spacing: 4) {
                    HStack {
                        // other image code ...
                        Spacer()
                        //DateTimeView(timestamp: post.timestamp)
                    }
                }
            }
            .background(
                displayImageView(for: post)
                    //.scaleEffect(1.13)
                    //.aspectRatio(contentMode: .fill)
                    //.frame(minWidth: 0, maxWidth: .infinity)
                    //.scaledToFill()
                    //.scaledToFit() // This will scale the image to fit the space without cropping
                    //.blur(radius: 3)
                    .opacity(0.4)
            )
            //.cornerRadius(10)
            //.clipped()
        }
    }


    @ViewBuilder
    private func displayImageView(for post: test2) -> some View {
        if let imageName = post.localDisplayImagePath {
            let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
            let imagePath = documentsDirectory?.appendingPathComponent(imageName).path
            
            if let imagePath = imagePath, let displayImage = UIImage(contentsOfFile: imagePath) {
                Image(uiImage: displayImage)
                    .resizable()
                    .aspectRatio(contentMode: .fill)
            } else if let displayUrl = post.displayUrl {
                URLImageView(url: displayUrl, isCircle: false, size: 100)
            } else {
                Image(systemName: "photo")
            }
        } else {
            Image(systemName: "photo")
        }
    }
}

推荐答案

假设PostViewList的行,您可以使用listRowBackground而不是background.

VStack(alignment: .leading, spacing: 4) {
    // ...
}
// note that this should be applied to the "outermost" view.
// in your code, it's the VStack, not the HStack
.listRowBackground(displayImageView(for: post).opacity(0.4))

Swift相关问答推荐

多个提取来计算核心数据中的记录

如何让CTFontCreateUIFontForLanguage与汉字和表情包协同工作?

在SWIFTUI&39;S视图修改器中使用等待关键字

为什么Swift可以在没有足够信息的情况下推断类型?

在Swift中如何用变量计算0.9的立方

(Swift)混淆了函数内 for-in 循环的返回值和循环后(函数内)的返回值)

TextField 键入未完成

并发执行代码中捕获的 var 的变异

LeetCode 249. 分组移位字符串

这是 Int64 Swift Decoding 多余的吗?

SwiftUI - Select 器没有 Select 值

使 Picker 与其他 BinaryInteger 类型兼容

如何在 ZStack 中单独下移卡片?

如何删除标签和步进按钮之间的间距是 SwiftUI Stepper?

让我的函数计算数组 Swift 的平均值

如何在界面生成器中创建 UILayoutGuide?

在 Swift 4 中,如何删除基于块的 KVO 观察者?

如何在今天的扩展(iOS)中访问 CoreData 模型

无法推断泛型参数的参数

两个 Date 对象之间的所有日期 (Swift)