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

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

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

当前回答

您可以使用resized()属性,但请记住,您不能在普通修饰符中使用resized,因此您必须使用Image扩展来实现它。

extension Image {
    func customModifier() -> some View {
        self
            .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)
 }

希望能有所帮助。

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

是这样的:

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

您需要添加. resized修饰符,以便能够更改图像的大小

代码看起来是这样的:

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

您可以使用resized()属性,但请记住,您不能在普通修饰符中使用resized,因此您必须使用Image扩展来实现它。

extension Image {
    func customModifier() -> some View {
        self
            .resizable()
            .aspectRatio(contentMode: .fit)
    }

204

如果我们在Assets.xcassets中有一个大图像。然后调整大小 首先,我们必须使用 resized(),然后使用frame()。

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