5 - Add an Image View in SwiftUI

SwiftUI makes it incredibly easy to add images to your app’s user interface. In this cookbook entry, you’ll learn how to add an Image View in SwiftUI and customize it to fit your app’s design.

Here’s an example. Drag your profile image into Assets.xcassets, name it Profile, then replace the ContentView struct in ContentView.swift with the following:

struct ContentView: View {
  var body: some View {
    Image("Profile")
      .resizable()
      .aspectRatio(contentMode: .fit)
      .frame(width: 200, height: 200)
      .clipShape(Circle())
      .overlay(Circle().stroke(Color.white, lineWidth: 4))
      .shadow(radius: 7)
  }
}

The preview should look like the following:

A SwiftUI image view with modifiers.

In this example, we’ve added an Image view that displays an image named “Profile”, and then applied a variety of view modifiers to it:

By chaining together these modifiers in a way that makes sense for your design, you can quickly customize the appearance of your image view.

For more details, see the “Images & Icons in SwiftUI” section in this cookbook