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

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

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

当前回答

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

希望能有所帮助。

其他回答

理解代码的逻辑结构是非常重要的。就像在SwiftUI中一样,默认情况下图像是不能调整大小的。因此,要调整任何图像的大小,您必须在声明image视图后立即应用. resized()修饰符使其可调整大小。

Image("An Image file name")
    .resizable()

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

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

在图像名称后使用. resized()方法。 确保在进行任何修改之前需要声明. resized()的用法。

是这样的:

Image("An Image file name")
    .resizable()
//add other modifications here 

使用下面的代码来渲染一个合适的宽高比和裁剪边界的图像:

struct ContentView: View {
    var body: some View {
        Image("donuts")
            .resizable()
            .scaledToFill()
            .frame(width: 200, height: 200)
            .border(Color.pink)
            .clipped()
    }
}

结果:

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

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