I have a large image in Assets.xcassets. How to resize this image with SwiftUI to make it small?
I tried to set frame but it doesn\'t work:
Image(roo
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)
By default, image views automatically size themselves to their contents, which might make them go beyond the screen. If you add the resizable() modifier then the image will instead automatically be sized so that it fills all the available space:
Image("example-image")
.resizable()
However, that may also cause the image to have its original aspect ratio distorted, because it will be stretched in all dimensions by whatever amount is needed to make it fill the space.
If you want to keep its aspect ratio you should add an aspectRatio modifier using either .fill or .fit, like this:
Image("example-image")
.resizable()
.aspectRatio(contentMode: .fit)
Note : 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
}
If you want to use aspect ratio with 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)
You should use .resizable()
before applying any size modifications on an Image
.
Image(room.thumbnailImage).resizable()
.frame(width: 32.0, height: 32.0)
You can define Image Properties as follow:-
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