【问题标题】:Set fixed size to Image将固定大小设置为图像
【发布时间】:2019-06-04 22:04:08
【问题描述】:

如何在 SwiftUI 中为图像设置固定大小?目前我的图片真的很大

目前我有这个结构:

struct TweetCell: View {
     var profileImage: Image
     var body: some View {
         HStack {
              profileImage
             VStack {
            ...
         }
    }
  }
}

【问题讨论】:

    标签: ios swift user-interface swiftui xcode11


    【解决方案1】:

    一种方法是通过

    profileImage.resizable().frame(width: 50, height: 50)
    

    【讨论】:

    • 不调用 resizable() 有什么区别?
    • 试试吧!而不是调整图像大小,而是从中裁剪出一个 50x50 的矩形
    • 您还可以添加.scaledToFit() 或.scaledToFill(),具体取决于您希望具有不同于框架的宽高比的图像填充视图的方式。适合保留纵横比,填充会扭曲图像。
    【解决方案2】:
    There are multiple ways to set fixed size to Image
    
    1) Image("yourImage").frame(width: 25, height: 25, alignment: .center)
    
    2) Image("yourImage").resizable().frame(width: 25, height: 25, alignment: .center)
    3) Image("yourImage").frame(width: 25, height: 25, alignment: .center).clipped()
    
    Attached the screenshots for all.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多