我在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)
当前回答
204
如果我们在Assets.xcassets中有一个大图像。然后调整大小 首先,我们必须使用 resized(),然后使用frame()。
Image(room.thumbnailImage)
.resizable()
.frame(width: 32.0, height: 32.0)
其他回答
如果你想使用纵横比调整大小,那么你可以使用以下代码:
Image(landmark.imageName).resizable()
.frame(width: 56.0, height: 56.0)
.aspectRatio(CGSize(width:50, height: 50), contentMode: .fit)
您需要添加. resized修饰符,以便能够更改图像的大小
代码看起来是这样的:
Image(room.thumbnailImage)
.resizable()
.frame(width: 32.0, height: 32.0)
在对图像应用任何大小修改之前,应该使用. resized()。
Image(room.thumbnailImage)
.resizable()
.frame(width: 32.0, height: 32.0)
如果你想在swiftUI中调整图像大小,只需使用以下代码:
import SwiftUI
struct ImageViewer : View{
var body : some View {
Image("Ssss")
.resizable()
.frame(width:50,height:50)
}
}
但这里有个问题。 如果你在一个按钮中添加这个图像,图像将不会显示,只有一个蓝色块会在那里。 要解决这个问题,只需这样做:
import SwiftUI
struct ImageViewer : View{
var body : some View {
Button(action:{}){
Image("Ssss")
.renderingMode(.original)
.resizable()
.frame(width:50,height:50)
}
}
}
为了使图像缩放到适合当前视图,我们使用resized()修饰符,它可以调整图像的大小以适应可用的空间。
例如:
Image("ImageName")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 200, height: 200, alignment: .center)