我在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("\(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
其他回答
在图像名称后使用. resized()方法。 确保在进行任何修改之前需要声明. resized()的用法。
是这样的:
Image("An Image file name")
.resizable()
//add other modifications here
Image(room.thumbnailImage)
.resizable()
.frame(width: 32.0, height: 32.0,alignment: .center)
在SwiftUI . resized()属性帮助调整图像的大小。 之后我们可以定制一些尺寸。
在SwiftUI中,使用. resized()方法来调整图像的大小。通过使用. aspectratio()并指定内容模式,您可以根据需要“适合”或“填充”图像。
例如,下面是通过拟合来调整图像大小的代码:
Image("example-image")
.resizable()
.aspectRatio(contentMode: .fit)
好吧,在SwiftUI中似乎很简单/按照他们给出的演示:https://developer.apple.com/videos/play/wwdc2019/204
struct RoomDetail: View {
let room: Room
var body: some View {
Image(room.imageName)
.resizable()
.aspectRatio(contentMode: .fit)
}
希望能有所帮助。
另一种方法是使用scaleEffect修饰符:
Image(room.thumbnailImage)
.resizable()
.scaleEffect(0.5)