Swift

How to resize Image with SwiftUI

25 September 2026 · 4 min read

How to resize Image with SwiftUI

Resizing images within your SwiftUI apps is a fundamental skill for any developer. Whether you’re building an image editor, a social media platform, or simply displaying images beautifully within your user interface, mastering image resizing is crucial. This comprehensive guide will walk you through several techniques for resizing images in SwiftUI, from basic scaling to more advanced methods, allowing you to tailor your approach to the specific needs of your project. Learn how to control aspect ratios, handle different image formats, and optimize for performance. Let’s dive in and unlock the power of image manipulation in your SwiftUI applications.

The Basics: Resizing with resizable() and scaledToFit()

SwiftUI provides a straightforward approach to resizing images using the .resizable() and .scaledToFit() modifiers. This combination allows an image to resize to fit its containing view while maintaining its original aspect ratio. This prevents distortion and ensures your images look their best. It’s perfect for situations where you want the image to fill the available space without cropping.

For instance, if you have an image within an HStack, applying these modifiers will resize the image to fit horizontally within the stack. This simple yet powerful method is often the quickest solution for many common image resizing scenarios.

Let’s see an example:

Image("YourImageName") .resizable() .scaledToFit() .frame(width: 200, height: 150) 

Controlling Aspect Ratio with aspectRatio()

Maintaining the correct aspect ratio is vital for preventing image distortion. SwiftUI’s .aspectRatio() modifier offers fine-grained control over how an image is resized. You can specify the desired aspect ratio explicitly or use .fit and .fill to control how the image fills or fits within its frame. .fill will fill the entire frame, potentially cropping parts of the image, while .fit ensures the entire image is visible, potentially adding letterboxing.

Understanding the nuances of aspect ratio control empowers you to create visually appealing layouts, especially when dealing with images of varying dimensions.

Example:

Image("YourImageName") .resizable() .aspectRatio(contentMode: .fit) // or .fill .frame(width: 200, height: 150) 

Advanced Resizing with resized(to:) (iOS 15 and later)

For more precise control over image dimensions, iOS 15 introduced the .resized(to:) modifier. This allows you to specify the exact width and height for your resized image, giving you pixel-perfect control. This is particularly useful when you need to resize images to specific dimensions for thumbnails or previews.

This newer method offers a more direct way to manage image resizing and is a valuable addition to the SwiftUI toolkit.

Image("YourImageName") .resizable() .resized(to: CGSize(width: 100, height: 100)) 

Resizing Images While Preserving Performance

Resizing large images can impact performance. Consider using techniques like downsampling to reduce the image size before displaying it in your UI. Libraries like SDWebImage can help manage image downloading and resizing efficiently, especially when dealing with remote images.

Optimizing image handling ensures a smooth and responsive user experience, even when working with high-resolution imagery.

  • Use downsampling for better performance.
  • Consider using third-party libraries for optimized image loading and resizing.

Infographic Placeholder: [Infographic visualizing different resizing techniques and their impact on image dimensions and aspect ratio]

  1. Import the necessary frameworks.
  2. Choose the appropriate resizing modifier.
  3. Implement the chosen method in your SwiftUI view.

See this guide for more information on SwiftUI image handling.

  • SwiftUI’s flexibility makes image resizing easy to implement.
  • Choose the right technique based on your project’s specific requirements.

By understanding these techniques, you can ensure your images are displayed beautifully and efficiently within your SwiftUI apps. Remember to choose the method that best suits your needs, taking into account aspect ratio, performance considerations, and the desired level of control over the final image size.

Ready to take your SwiftUI image skills to the next level? Explore advanced topics like custom image filters and transformations to create truly unique and engaging user interfaces. Learn more about image optimization strategies for enhanced performance and user experience. Consider experimenting with different approaches and refining your techniques to achieve pixel-perfect results in your image-rich SwiftUI applications.

Question & Answer :
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(room.thumbnailImage) .frame(width: 32.0, height: 32.0) 

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

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