ios 如何使用 SwiftUI 调整图像大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/56505692/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 09:54:28  来源:igfitidea点击:

How to resize Image with SwiftUI?

iosswiftswiftui

提问by subdan

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

我在 Assets.xcassets 中有一个大图像。如何使用 SwiftUI 调整此图像的大小以使其变小?

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

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

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

回答by rraphael

You should use .resizable()before applying any size modifications on an Image.

您应该.resizable()在对Image.

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

回答by Confused Vorlon

How about this:

这个怎么样:

struct ResizedImage: View {
    var body: some View {

            Image("myImage")
                .resizable()
                .scaledToFit()
                .frame(width: 200.0,height:200)

    }
}

the image view is 200x200, but the image maintains the original aspect ratio (rescaling within that frame)

图像视图为 200x200,但图像保持原始纵横比(在该帧内重新缩放)

回答by Womble

Expanding on @rraphael's answer and comments:

扩展@rraphael 的回答和评论:

As of Xcode 11 beta 2, you can scale an image to arbitrary dimensions, while maintaining the original aspect ratio by wrapping the image in another element.

从 Xcode 11 beta 2 开始,您可以将图像缩放到任意尺寸,同时通过将图像包装在另一个元素中来保持原始纵横比。

e.g.

例如

struct FittedImage: View
{
    let imageName: String
    let width: CGFloat
    let height: CGFloat

    var body: some View {
        VStack {
            Image(systemName: imageName)
                .resizable()
                .aspectRatio(1, contentMode: .fit)
        }
        .frame(width: width, height: height)
    }
}


struct FittedImagesView: View
{
    private let _name = "checkmark"

    var body: some View {

        VStack {

            FittedImage(imageName: _name, width: 50, height: 50)
            .background(Color.yellow)

            FittedImage(imageName: _name, width: 100, height: 50)
            .background(Color.yellow)

            FittedImage(imageName: _name, width: 50, height: 100)
            .background(Color.yellow)

            FittedImage(imageName: _name, width: 100, height: 100)
            .background(Color.yellow)

        }
    }
}

Results

结果

Fitted images preserving aspect ratio

拟合图像保留纵横比

(For some reason, the image is showing as a bit blurry. Rest assured that the real output is sharp.)

(出于某种原因,图像显示有点模糊。请放心,实际输出是清晰的。)

回答by amp.dev

use .resizable() for resize image in SwiftUI

使用 .resizable() 在 SwiftUI 中调整图像大小

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

回答by dinesh sharma

If you want to use aspect ratiowith resizing then you can use following code:

如果要在调整大小时使用纵横比,则可以使用以下代码:

Image(landmark.imageName).resizable()
                .frame(width: 56.0, height: 56.0)
                .aspectRatio(CGSize(width:50, height: 50), contentMode: .fit)

回答by rust

Well, It's seemspretty easy in SwiftUI/ Following the demo they given : https://developer.apple.com/videos/play/wwdc2019/204

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

Hope it helps.

希望能帮助到你。

回答by sachin jeph

If you want to resize the image in swiftUI just use the following code :

如果要在 swiftUI 中调整图像大小,只需使用以下代码:

import SwiftUI

    struct ImageViewer : View{
        var body : some View {
            Image("Ssss")
            .resizable()
            .frame(width:50,height:50)
        }
    }

But here is problem with this. If you add this Image inside a Button, the Image will not be shown, just a block of blue colour would be there. To solve this issue, just do this :

但是这里有问题。如果您将此图像添加到按钮中,则不会显示图像,只会显示一块蓝色。要解决此问题,只需执行以下操作:

import SwiftUI

struct ImageViewer : View{
    var body : some View {
        Button(action:{}){
        Image("Ssss")
        .renderingMode(.original)
        .resizable()
        .frame(width:50,height:50)
    }
   }
}

回答by cmlcrn17

My image name is img_Logo and you can change image name define image properties this: 

 VStack(alignment: .leading, spacing: 1) {
                        //Image Logo Start
                        Image("img_Logo")
                            .resizable()
                            .padding(.all, 10.0)
                            .frame(width: UIScreen.main.bounds.width * 0.4, height: UIScreen.main.bounds.height * 0.2)
                        //Image Logo Done
                    }

回答by Hanny

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)

回答by ShigaSuresh

Use below code.

使用下面的代码。

        Image("image")
        .resizable()
        .frame(width: 60, height: 60, alignment: .leading)
        .cornerRadius(30.0)