Skip to content

Instantly share code, notes, and snippets.

@Jomy10
Last active November 11, 2021 21:16
Show Gist options
  • Save Jomy10/699b299e5827ad4b91b2a2d136b6f3f9 to your computer and use it in GitHub Desktop.
Save Jomy10/699b299e5827ad4b91b2a2d136b6f3f9 to your computer and use it in GitHub Desktop.
SwiftUI Image Share Sheet
import Foundation
import SwiftUI
/// A view for sharing an image. The user can add the image to their cameraroll, share it via iMessage, etc.
struct ImageShareSheet: UIViewControllerRepresentable {
/// The images to share
let images: [UIImage]
func makeUIViewController(context: Context) -> some UIViewController {
let activityViewController = UIActivityViewController(activityItems: images, applicationActivities: nil)
// exclude some activity types from the list (optional)
// activityViewController.excludedActivityTypes = [ UIActivity.ActivityType.airDrop, UIActivity.ActivityType.postToFacebook ]
return activityViewController
}
func updateUIViewController(_ uiViewController: UIViewControllerType, context: Context) {
}
}
extension View {
func imageShareSheet(
isPresented: Binding<Bool>,
images: [UIImage]
) -> some View {
return sheet(isPresented: isPresented, content: { ImageShareSheet(images: images) } )
}
func imageShareSheet(
isPresented: Binding<Bool>,
image: UIImage
) -> some View {
return sheet(isPresented: isPresented, content: { ImageShareSheet(images: [image]) } )
}
}
// Usage
struct ExampleView: View {
@State var showImageSheet = false
var body: some View {
Button("Share image") {
showImageSheet = true
}
.imageShareSheet(isPresented: $showImageSheet, image: UIImage(named: "example_image"))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment