我在Assets.xcassets中有一个大图像。如何用SwiftUI调整这张图片的大小使它变小?
我试着设置框架,但它不起作用:
Image(room.thumbnailImage)
.frame(width: 32.0, height: 32.0)
我在Assets.xcassets中有一个大图像。如何用SwiftUI调整这张图片的大小使它变小?
我试着设置框架,但它不起作用:
Image(room.thumbnailImage)
.frame(width: 32.0, height: 32.0)
当前回答
在图像视图上使用带有动态类型的字体修饰符:
Image(systemName: "nose")
.font(.largeTitle)
其他回答
SwiftUI为我们提供了. resized()修饰符,它可以让SwiftUI调整图像的大小以适应其空间
struct ContentView: View {
var body: some View {
Image("home")
.antialiased(true) //for smooth edges for scale to fill
.resizable() // for resizing
.scaledToFill() // for filling image on ImageView
}
}
注意:我的图像名称是img_Logo,你可以改变图像名称定义图像属性:
VStack(alignment: .leading, spacing: 1) {
//Image Logo Start
Image("img_Logo")
.resizable()
.padding(.all, 10.0)
.frame(width: UIScreen.main.bounds.width * 0.4, height: UIScreen.main.bounds.height * 0.2)
//Image Logo Done
}
您需要添加. resized修饰符,以便能够更改图像的大小
代码看起来是这样的:
Image(room.thumbnailImage)
.resizable()
.frame(width: 32.0, height: 32.0)
在对图像应用任何大小修改之前,应该使用. resized()。
Image(room.thumbnailImage)
.resizable()
.frame(width: 32.0, height: 32.0)
“图像属性”的定义如下:—
Image("\(Image Name)")
.resizable() // Let you resize the images
.frame(width: 20, height: 20) // define frame size as required
.background(RoundedRectangle(cornerRadius: 12) // Set round corners
.foregroundColor(Color("darkGreen")) // define foreground colour