In SwiftUI, you can easily display images in your views using the Image view. To get started, you’ll need to add the image to a new image set in your Assets.xcassets file in Xcode.

Add Image to Assets.xcassets

Once you have added the image to your assets, you can use the Image view in your ContentView like this:

import SwiftUI

struct ContentView: View {
    var body: some View {
        Image("Avatar")
    }
}

Displaying an Image

You can also use the Image view to display system images by using the Image(systemName:) initializer:

struct ContentView: View {
    var body: some View {
        Image(systemName: "house")
    }
}

Displaying a System Image

The Image view provides a set of modifiers that you can use to customize the appearance of the image, such as:

  • .resizable() to make the image resizable and adjust to the .frame() dimensions
  • .frame() to set the width and height of the image
  • .clipShape() to apply a clipping shape to the image
  • .border() to set the color of the border around the image
  • .overlay() to add another view on top of the image
  • .aspectRatio() to set the aspect ratio of the image
  • .clipped() to clip the image to fit within the frame

Here’s an example that demonstrates some of these modifiers in action:

Image with Modifiers

Tags: SwiftUI, Images, Xcode, View, Modifiers