How to resize Image with SwiftUI?
我在Assets.xcassets中拥有很大的形象。 如何使用SwiftUI调整图像大小以缩小图像?
我试图设置框架,但不起作用:
1 2 | Image(room.thumbnailImage) .frame(width: 32.0, height: 32.0) |
在
1 2 | Image(room.thumbnailImage).resizable() .frame(width: 32.0, height: 32.0) |
这个怎么样:
1 2 3 4 5 6 7 8 9 10 | struct ResizedImage: View { var body: some View { Image("myImage") .resizable() .scaledToFit() .frame(width: 200.0,height:200) } } |
图像视图为200x200,但是图像保持原始宽高比(在该帧内缩放)
扩展@rraphael的答案和评论:
从Xcode 11 beta 2开始,您可以将图像缩放到任意尺寸,同时通过将图像包装在另一个元素中来保持原始宽高比。
例如
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | 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) } } } |
结果
(由于某种原因,图像显示有点模糊。请确保实际输出清晰。)
在SwiftUI中使用.resizable()调整图像大小
1 2 3 | Image("example-image") .resizable() .aspectRatio(contentMode: .fit) |
Note : My image name is
img_Logo and you can change image name define image properties this:
1 2 3 4 5 6 7 8 | 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 } |
由于我们不应该硬编码/固定图像大小。这是提供更好的范围以根据不同设备上的屏幕分辨率进行调整的更好方法。
1 2 3 4 5 6 | 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) |
如果要在调整大小时使用宽高比,则可以使用以下代码:
1 2 3 | Image(landmark.imageName).resizable() .frame(width: 56.0, height: 56.0) .aspectRatio(CGSize(width:50, height: 50), contentMode: .fit) |
另一种方法是使用
1 2 3 | Image(room.thumbnailImage) .resizable() .scaleEffect(0.5) |
1 2 3 4 5 6 7 8 9 10 11 | struct AvatarImage: View { var body: some View { Image("myImage") .resizable() .scaledToFill() // <=== Saves aspect ratio .frame(width: 60.0, height:60) .clipShape(Circle()) } } |
如果要在swiftUI中调整图像大小,请使用以下代码:
1 2 3 4 5 6 7 8 9 | import SwiftUI struct ImageViewer : View{ var body : some View { Image("Ssss") .resizable() .frame(width:50,height:50) } } |
但是这里有问题。
如果将此图像添加到按钮内,则不会显示该图像,仅会出现蓝色的块。
要解决此问题,只需执行以下操作:
1 2 3 4 5 6 7 8 9 10 11 12 | import SwiftUI struct ImageViewer : View{ var body : some View { Button(action:{}){ Image("Ssss") .renderingMode(.original) .resizable() .frame(width:50,height:50) } } } |
好吧,在SwiftUI中似乎很容易/按照他们给出的演示进行操作:https://developer.apple.com/videos/play/wwdc2019/204
1 2 3 4 5 6 7 8 | struct RoomDetail: View { let room: Room var body: some View { Image(room.imageName) .resizable() .aspectRatio(contentMode: .fit) } |
希望能帮助到你。
理解代码的逻辑结构非常重要。像在SwiftUI中一样,图片默认情况下无法调整大小。因此,要调整任何图像的大小,必须在声明"图像"视图后立即应用.resizable()修饰符使其可调整大小。
1 2 | Image("An Image file name") .resizable() |
您可以定义图像属性,如下所示:
1 2 3 4 5 | Image("\\(Image Name)") .resizable() // Let you resize the images .frame(width: 20, height: 20) // define frame size as required .background(RoundedRectangle(cornerRadius: 12) // Set round corners .foregroundColor(Color("darkGreen")) // define foreground colour |
使用以下代码。
1 2 3 4 | Image("image") .resizable() .frame(width: 60, height: 60, alignment: .leading) .cornerRadius(30.0) |