SwiftUI Circular Image Tutorial

By applying a clipping shape to a view a part of an image will be drawn inside this shape. In this tutorial a circular image will be displayed. SwiftUI requires Xcode 11 and MacOS Catalina, for which the Betas can be downloaded at the Apple developer portal.

Open Xcode and either click Create a new Xcode project in Xcode’s startup window, or choose File > New > Project. In the template selector, select iOS as the platform, select the Single View App template, and then click Next. Enter SwiftUICircularImageTutorial as the Product Name, select the Use SwiftUI checkbox, and click Next. Choose a location to save the project on your Mac.

An image is needed for this tutorial, download the image and drag it from the finder to the Assets library.

In the canvas, click Resume to display the preview. If the canvas isn’t visible, select Editor > Editor and Canvas to show it.

In the Project navigator, click to select ContentView.swift. Change the code inside the ContentView struct to

struct ContentView: View {
    var body: some View {
        Image("swiftui-logo")
            .clipShape(Circle())
            .shadow(radius: 10)
            .overlay(Circle().stroke(Color.red, lineWidth: 5))
    }
}

The image will be drawn inside a circular clip and with a shadow of 10 points. Outside the circle a red line will be drawn as outline.

The preview will look like this.

The source code of the SwiftUICircularImageTutorial can be downloaded at the ioscreator repository on Github.