SwiftUI - Customize Image View



Image view displays single or multiple images together on the screen. It is the most important view of SwiftUI. Using image view we can easily display images from the asset catalog. It can work easily with various image formats such as PNG, JPEG, PDF, etc. It can also work efficiently with SFSymbols.

Image view effortlessly manages and manipulates images to create more creative layouts. It also supports various modifiers to stretch, scale, resize, or pin images to a particular location. Image view can only display images it is not interactive.

Displaying Image using Image View in SwiftUI

Displaying an image using Image view is a very straightforward method we need to follow the following steps to display an image on the screen−

Step 1: Prepare Image

To display the image in the image view first we need to add the image inside the Assets.xcassets folder by dragging and dropping the image file.

Step 2: Add Image View

To add an image in the view, open ContentView.swift file then use Image view inside the body property. As we already placed the image inside the assets we pass the reference of the image inside the Image view. It reduces the risk of runtime errors happening due to incorrect image names. If any changes are made in the asset catalog, then it will notify in the code where the image is used as a reference so that you can change it before compiling the code.

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

Step 3: Run the App

Run the app in the simulator or device to see the image.

Example

The following SwiftUI program is used to create an image view.

import SwiftUI

struct ContentView: View {
   var body: some View {
      Image(.wallpaper)
   }
}

#Preview {
   ContentView()
}

Output

Customize Image View

Customize Image View in SwiftUI

By customizing the image view we can change the appearance and behavior of the image according to the design requirement with the help of various modifiers provided by SwiftUI. Using these modifiers we can change the shape, size, color, and other visual properties of the image view. SwiftUI provide various in-built modifiers to customize the image present in the Image view and some of the commonly used modifiers are as follows −

Modifiers Description
frame(width:height:alignment:) It is used to explicitly set the width and height of the image view.
resizable() It is used to resize images.
aspectRation(contentMode:) It is used to manage the aspect ratio of the image while resizing it.
clipShape(RoundedRectangle(cornerRadius:)) It is used to round the corners of the image view. This modifier is also used to change the shape of the image.
padding() It is used to insert space around the image view
Image(systemName:"SymbolName") It is used to display SF Symbol.
shadow(radius) It is used to apply a shadow effect on the image view. overlay() It is used to overlay another view on top of the image. opacity() It is used to set the opacity of the image view.

Example

The following SwiftUI program is used to customize image view.

import SwiftUI

struct ContentView: View {
   var body: some View {
      VStack{
         Image(.wallpaper)
            .resizable()
            .frame(width: 300, height: 200)
         Image(.wallpaper)
            .resizable()
            .frame(width: 300, height: 200)
            .clipShape(Capsule())
         Image(.wallpaper)
            .resizable()
            .frame(width: 300, height: 200)
            .shadow(radius: 30)
            .opacity(0.7)
         Image(systemName: "star")
            .frame(width: 300, height: 200)
            .font(.largeTitle)           
      }
   }
}

#Preview {
   ContentView()
}

Output

Customize Image View
Advertisements