Skip to content

Instantly share code, notes, and snippets.

@tsuzukihashi
Created December 1, 2019 11:37
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tsuzukihashi/d08fce005a8d892741f4cf965533bd56 to your computer and use it in GitHub Desktop.
Save tsuzukihashi/d08fce005a8d892741f4cf965533bd56 to your computer and use it in GitHub Desktop.
SwiftUI ActivityViewController Share Image
import SwiftUI
struct TestPage: View {
@State private var rect: CGRect = .zero
@State private var uiImage: UIImage? = nil
@State private var showShareSheet = false
var body: some View {
VStack {
HStack {
Image(systemName: "sun.haze")
.font(.title)
.foregroundColor(.white)
Text("Hello, World!")
.font(.title)
.foregroundColor(.white)
}
.padding()
.background(Color.blue)
.cornerRadius(8)
.background(RectangleGetter(rect: $rect))
.onAppear() {
}
Button(action: {
self.uiImage = UIApplication.shared.windows[0].rootViewController?.view!.getImage(rect: self.rect)
self.showShareSheet.toggle()
}) {
Image(systemName: "square.and.arrow.up")
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 50, height: 50)
.padding()
.background(Color.pink)
.foregroundColor(Color.white)
.mask(Circle())
}.sheet(isPresented: self.$showShareSheet) {
ShareSheet(photo: self.uiImage!)
}.padding()
}
}
}
struct TestPage_Previews: PreviewProvider {
static var previews: some View {
TestPage()
}
}
struct RectangleGetter: View {
@Binding var rect: CGRect
var body: some View {
GeometryReader { geometry in
self.createView(proxy: geometry)
}
}
func createView(proxy: GeometryProxy) -> some View {
DispatchQueue.main.async {
self.rect = proxy.frame(in: .global)
}
return Rectangle().fill(Color.clear)
}
}
extension UIView {
func getImage(rect: CGRect) -> UIImage {
let renderer = UIGraphicsImageRenderer(bounds: rect)
return renderer.image { rendererContext in
layer.render(in: rendererContext.cgContext)
}
}
}
import LinkPresentation
struct ShareSheet: UIViewControllerRepresentable {
let photo: UIImage
func makeUIViewController(context: Context) -> UIActivityViewController {
let text = "🐥"
let itemSource = ShareActivityItemSource(shareText: text, shareImage: photo)
let activityItems: [Any] = [photo, text, itemSource]
let controller = UIActivityViewController(
activityItems: activityItems,
applicationActivities: nil)
return controller
}
func updateUIViewController(_ vc: UIActivityViewController, context: Context) {
}
}
class ShareActivityItemSource: NSObject, UIActivityItemSource {
var shareText: String
var shareImage: UIImage
var linkMetaData = LPLinkMetadata()
init(shareText: String, shareImage: UIImage) {
self.shareText = shareText
self.shareImage = shareImage
linkMetaData.title = shareText
super.init()
}
func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
return UIImage(named: "AppIcon ") as Any
}
func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
return nil
}
func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
return linkMetaData
}
}
@PankajGaikar
Copy link

I have been failing to make a share sheet work on my SwiftUI project but thanks to you. This code worked perfectly for me.

@SAWesty
Copy link

SAWesty commented Aug 18, 2022

Thank you for this code sample. I have been trying to make it work, but "ShareSheet(photo: self.uiImage!)" always fails. It returns "Fatal error: Unexpectedly found nil while unwrapping an Optional value", even though I have verified self.uiImage in not nil (using cgImage and ciImage). Do I need to declare the variable differently to pass into the Share Sheet? I am on iOS 15.5, but I have tried using down to 14 with same result. Any help would be appreciated. Your code snippet is the closest to what I am working towards. Thanks.

@angelinaFri
Copy link

thank you! Did really help me!

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