How to Share Images in SwiftUI?

In the tutorial, you'll learn to share images in your iOS application with only a few lines of swift code.

Share:

Mar 10, 2023 101 Words

Read Time: 1 Minutes

A screenshot of iOS application using ShareLink view to share images built with SwiftUI

Introduction

In SwiftUI, you can now use the new ShareLink1 view to share links and images using Apple’s ‘sharesheet’ option in only a few lines of swift code.

This features pairs well with SharePreview 2 that allows the user to view the image preview while the share-sheet is in action.

Code Snippet

import SwiftUI

struct ContentView: View {

    @State var image = Image("Image")

    var body: some View {
        VStack {
            Spacer()
            image.resizable().aspectRatio(contentMode: .fit)
            Spacer()
            ShareLink(
                item: image,
                preview: SharePreview(
                    "Share Preview",
                    image: image
                )
            )
            Spacer()
        }
    }
}

Demonstration

A screenshot of ShareLink View on iPhone in Landscape view

A screenshot of ShareLink in action on iPhone in Landspace view

References