iOS13 share sheet: how to set preview thumbnail when sharing UIImage
Just pass the image urls to UIActivityViewController
not the UIImage
objects.
For example:
let imageURLs: [URL] = self.prepareImageURLs()
let activityViewController = UIActivityViewController(activityItems: imageURLs, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
You can see that the image name and the image properties are shown in the top of the UIActivityViewController
. Hope it helps!
How to attach an image icon to the `UIActivityViewController`?
You need to define the LinkMetadata
using activityViewControllerLinkMetadata
method:
@available(iOS 13.0, *)
func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
let metadata = LPLinkMetadata()
// define your metadata here
return metadata
}
More info here: https://developer.apple.com/documentation/uikit/uiactivityitemsource/3144571-activityviewcontrollerlinkmetada?language=swift
iOS 13 Share Sheet: Changing subtitle/item description
This is really silly, but this is how to do it.
metadata.originalURL = URL(fileURLWithPath: "whatever description you want to put")
And as a note, you don't have to create the file/folder itself.
How to show URL icon with text in UIActivityViewController?
There is a difference between sharing data and showing data - I think in your case, showing the data that's about to be shared.
I could be wrong, but if not, you are actually able to share the data - in this case a UIImage
and text - but wish to display what is about to be shared. I had this issue a while back. Here's my code. See if it can help you - basically you need to add metadata to UIActivityViewController
delegate.
func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
let shareImage = preview.uiImage.adjustedForShareSheetPreviewIconProvider()
let imageProvider = NSItemProvider(object: shareImage)
let metadata = LPLinkMetadata()
metadata.imageProvider = imageProvider
metadata.title = "Title"
return metadata
}
Adjust shareImage
and title
as needed.
EDIT
I was using my code and was incomplete. adjustedForShareSheetPreviewIconProvider()
is part of UIImage
, and as such, it assumes this. In my code, I'm actually calling a GLKView
to return this UIImage
and then use it this way in the metadata.
So if your image is something returned from the URL as an icon and is (or can be converted to) a UIImage
, just add it to the metadata like my code above.
iOS 13, Custom Image, title and subtitle in the presented UIActivityViewController
I have found a solution using UIActivityItemSource
UIActivityItemSource
have this protocol activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata?
which we can use to set image title and subtitle for our UIActivityViewController
This is an example:
public func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
let metadata = LPLinkMetadata()
metadata.title = "My title" // Preview Title
// Set image
metadata.imageProvider = NSItemProvider(object: image)
metadata.iconProvider = NSItemProvider(object: image)
metadata.url = urlImage
// Set URL for sharing
metadata.originalURL = myUrl // Add this if you want to have a url in your share message.
return metadata
}
And this is the result: I have my custom image and title.
Related Topics
How to Get the Front Camera in Swift
Posting Photos to Facebook Fan Page with iOS Sdk
How to Convert Fbprofilepictureview to an Uiimage
Sprite Kit Pin Joints Appear to Have an Incorrect Anchor
How to Share Nsdata or Phasset Video Using Facebook iOS Sdk 4.0 Fbsdksharedialog
Enable and Debug Zombie Objects in iOS Using Xcode 5.1.1
How to Load Icarousel View from Storyboard or Xib
What Does the Property "Nonatomic" Mean
iOS 7 Uitableview: How to Remove Space Between Navigation Bar and First Cell
Selecting a Word in a Uitextview
Nsoperationqueue Serial Fifo Queue
Uisearchdisplaycontroller Without Dimming
Tracking Mkmapview Centercoordinate While Panning
Fetching Selected Attribute in Entities
Swift 3 JSON Nsfastenumerationiterator Has No Subscript Members