我在Assets.xcassets中有一个大图像。如何用SwiftUI调整这张图片的大小使它变小?
我试着设置框架,但它不起作用:
Image(room.thumbnailImage)
.frame(width: 32.0, height: 32.0)
我在Assets.xcassets中有一个大图像。如何用SwiftUI调整这张图片的大小使它变小?
我试着设置框架,但它不起作用:
Image(room.thumbnailImage)
.frame(width: 32.0, height: 32.0)
当前回答
如果你想在swiftUI中调整图像大小,只需使用以下代码:
import SwiftUI
struct ImageViewer : View{
var body : some View {
Image("Ssss")
.resizable()
.frame(width:50,height:50)
}
}
但这里有个问题。 如果你在一个按钮中添加这个图像,图像将不会显示,只有一个蓝色块会在那里。 要解决这个问题,只需这样做:
import SwiftUI
struct ImageViewer : View{
var body : some View {
Button(action:{}){
Image("Ssss")
.renderingMode(.original)
.resizable()
.frame(width:50,height:50)
}
}
}
其他回答
“图像属性”的定义如下:—
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
在对图像应用任何大小修改之前,应该使用. resized()。
Image(room.thumbnailImage)
.resizable()
.frame(width: 32.0, height: 32.0)
在图像名称后使用. resized()方法。 确保在进行任何修改之前需要声明. resized()的用法。
是这样的:
Image("An Image file name")
.resizable()
//add other modifications here
因为我们不应该硬编码/修复图像大小。这里提供了一种更好的方法,可以根据不同设备上的屏幕分辨率进行调整。
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)
这是山景,这是我的照片。
让我们在代码中创建一个简单的图像视图。
var body: some View {
Image(“mountains”)
}
结果看起来不太好。
让我们先调整它的大小,用一个刻度来适应它
var body: some View {
Image(“mountains”)
.resizable()
.scaledToFit()
}
现在看起来好多了。
注意,这里有空白,因为图像是垂直拍摄的,而屏幕是水平的。
不使用尺寸来填充。
图像的某些部分超出了屏幕,但它看起来比默认的没有任何比例更好。
如果你点击预览里面的图片,你会看到图片有多大。蓝色的线是图像的边界。