How to resize Image with SwiftUI?

前端 未结 14 626
清歌不尽
清歌不尽 2020-12-23 19:54

I have a large image in Assets.xcassets. How to resize this image with SwiftUI to make it small?

I tried to set frame but it doesn\'t work:

Image(roo         


        
14条回答
  •  长情又很酷
    2020-12-23 20:44

    By default, image views automatically size themselves to their contents, which might make them go beyond the screen. If you add the resizable() modifier then the image will instead automatically be sized so that it fills all the available space:

    Image("example-image")
        .resizable()
    

    However, that may also cause the image to have its original aspect ratio distorted, because it will be stretched in all dimensions by whatever amount is needed to make it fill the space.

    If you want to keep its aspect ratio you should add an aspectRatio modifier using either .fill or .fit, like this:

    Image("example-image")
        .resizable()
        .aspectRatio(contentMode: .fit)
    

提交回复
热议问题