我在Assets.xcassets中有一个大图像。如何用SwiftUI调整这张图片的大小使它变小?

我试着设置框架,但它不起作用:

Image(room.thumbnailImage)
    .frame(width: 32.0, height: 32.0)

当前回答

在图像视图上使用带有动态类型的字体修饰符:

Image(systemName: "nose")
        .font(.largeTitle)

其他回答

在对图像应用任何大小修改之前,应该使用. resized()。

Image(room.thumbnailImage)
    .resizable()
    .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)

在图像视图上使用带有动态类型的字体修饰符:

Image(systemName: "nose")
        .font(.largeTitle)

好吧,在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)
 }

希望能有所帮助。