我面临着一些小问题,调整自定义图像的大小,以完全填充列表行. 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")
}
}
}