我在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中似乎很简单/按照他们给出的演示: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)
}
希望能有所帮助。
其他回答
在SwiftUI中,使用. resized()方法来调整图像的大小。通过使用. aspectratio()并指定内容模式,您可以根据需要“适合”或“填充”图像。
例如,下面是通过拟合来调整图像大小的代码:
Image("example-image")
.resizable()
.aspectRatio(contentMode: .fit)
SwiftUI为我们提供了. resized()修饰符,它可以让SwiftUI调整图像的大小以适应其空间
struct ContentView: View {
var body: some View {
Image("home")
.antialiased(true) //for smooth edges for scale to fill
.resizable() // for resizing
.scaledToFill() // for filling image on ImageView
}
}
您可以使用resized()属性,但请记住,您不能在普通修饰符中使用resized,因此您必须使用Image扩展来实现它。
extension Image {
func customModifier() -> some View {
self
.resizable()
.aspectRatio(contentMode: .fit)
}
好吧,在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)
}
希望能有所帮助。
另一种方法是使用scaleEffect修饰符:
Image(room.thumbnailImage)
.resizable()
.scaleEffect(0.5)