How to resize Image with SwiftUI?

前端 未结 14 514
清歌不尽
清歌不尽 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:57

    In SwiftUI, use the .resizable() method to resize an image. By using .aspectRatio() and specifying a ContentMode, you can either "Fit" or "Fill" the image, as appropriate.

    For example, here is code that resizes the image by fitting:

    Image("example-image")
    .resizable()
    .aspectRatio(contentMode: .fit)
    
    0 讨论(0)
  • 2020-12-23 20:58

    Since we shouldn't hardcode/fix the image size. Here is a better way to provide range to adjust according to the screen's resolution on different devices.

    Image("ImageName Here")
           .resizable()
           .frame(minWidth: 60.0, idealWidth: 75.0, maxWidth: 95.0, minHeight: 80.0, idealHeight: 95.0, maxHeight: 110.0, alignment: .center)
           .scaledToFit()
           .clipShape(Capsule())
           .shadow(color: Color.black.opacity(5.0), radius: 5, x: 5, y: 5)
    
    0 讨论(0)
提交回复
热议问题