Sharing Images With UIActivityViewController & UIActivityItemSource | by Derek Kim | Medium


AI Summary Hide AI Generated Summary

Implementing Image Sharing in iOS

This article details how to create a button to share images using UIActivityViewController and UIActivityItemSource in iOS. The author shows a simple implementation where tapping a button presents the share sheet with an image.

Issues with Basic Implementation

The basic implementation has some issues: the image doesn't display correctly in the share sheet, and the title and subtitle are missing.

  • Image not showing in the share view.
  • Missing title and subtitle.

Further Development

The article promises to address these issues in subsequent sections (not included in this excerpt). The provided code snippet focuses on the initial, flawed implementation. This snippet shows how to present a UIActivityViewController with an image, but this image is not rendered as expected.

Sign in to unlock more AI features Sign in with Google
We located an Open Access version of this article, legally shared by the author or publisher. Open It

Sharing Images With UIActivityViewController & UIActivityItemSource

Hello, this is Derek!

Today I wanted to write about implementing a button to share an images with others. By the end of this blog, you will now understand how to implement sharing functionality using UIActivityViewController and UIActivityItemSource. See image example below for a visual example:

Suppose you have an app that has an image. I’ve gone ahead and created my own simple UI view for this particular demo.

When the button is pressed, I want to show the share view that was shown earlier. Let’s see what exactly we want to show on the sharing view.

We first have an image thumbnail on the left, with a title text, and a subtitle that consists the image’s file type and file size.

Let’s start with the basic implementation example. To share an image, we must present UIActivityViewController and pass the data to activityItems parameter.

public init(activityItems: [Any], applicationActivities: [UIActivity]?)

It takes an array of [Any], which means we can also pass other types such as String, Int or any other valid types.

shareButton.addAction(    UIAction { [weak self] _ in        guard let self = self else { return }        guard let image = imageView.image else { return }let images = [image]        let activityVC = UIActivityViewController(activityItems: images, applicationActivities: nil)        self.present(activityVC, animated: true)    }, for: .touchUpInside)

In this simple example, when the user taps the shareButton, it will present the activityVC, which is the UIActivityViewController with the images we have passed along. However, you’ll notice that this itself has couple of issues.

  1. Image is not showing in the actual sharing view
  2. We are missing the title, and the subtitle

🧠 Pro Tip

Skip the extension — just come straight here.

We’ve built a fast, permanent tool you can bookmark and use anytime.

Go To Paywall Unblock Tool
Sign up for a free account and get the following:
  • Save articles and sync them across your devices
  • Get a digest of the latest premium articles in your inbox twice a week, personalized to you (Coming soon).
  • Get access to our AI features

  • Save articles to reading lists
    and access them on any device
    If you found this app useful,
    Please consider supporting us.
    Thank you!

    Save articles to reading lists
    and access them on any device
    If you found this app useful,
    Please consider supporting us.
    Thank you!