Skip to content

Instantly share code, notes, and snippets.

@SwapnanilDhol
Created August 21, 2020 13:06
Show Gist options
  • Save SwapnanilDhol/90dba97b4f07c97ef9f6110534de26b2 to your computer and use it in GitHub Desktop.
Save SwapnanilDhol/90dba97b4f07c97ef9f6110534de26b2 to your computer and use it in GitHub Desktop.
A UIActivity Helper Class to show thumbnail preview and text in the share sheet title area
//
// ItemDetailSource.swift
// StickerTweet
//
// Created by Swapnanil Dhol on 7/9/20.
// Copyright © 2020 Swapnanil Dhol. All rights reserved.
//
import UIKit
import LinkPresentation
class ItemDetailSource: NSObject {
let name: String
let image: UIImage
init(name: String, image: UIImage) {
self.name = name
self.image = image
}
}
extension ItemDetailSource: UIActivityItemSource {
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
image
}
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
image
}
func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
let metaData = LPLinkMetadata()
metaData.title = name
metaData.imageProvider = NSItemProvider(object: image)
return metaData
}
}
//Code used for StickerCards. Please make changes as neccesaary.
private func saveCardWithBackground() {
guard let stickerImage = generateCardWithBackground() else { return }
let ac = UIActivityViewController(activityItems: [ItemDetailSource(name: "Tweet by \(tweet?.name ?? "Unknown")", image: stickerImage)], applicationActivities: nil)
if let popoverController = ac.popoverPresentationController {
popoverController.sourceView = self.view
popoverController.sourceRect = CGRect(x: self.view.bounds.midX,
y: self.view.bounds.midY,
width: 0,
height: 0)
popoverController.permittedArrowDirections = []
}
self.present(ac, animated: true)
}
@ZheDre1N
Copy link

Thanks, very helpfull

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment