Guest User

Untitled

a guest
Jul 12th, 2026
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. SwiftUI View → UIImage Snapshot + Share Sheet
  2. ==============================================
  3.  
  4. Renders any SwiftUI view into a UIImage, then presents the system share sheet
  5. with the image and accompanying text.
  6.  
  7. Usage:
  8.  
  9. func share() {
  10. let image = MyShareView()
  11. .frame(width: 380, height: 370)
  12. .snapshot
  13.  
  14. let text = """
  15. Check out this coin on MyApp.
  16. Download for free: https://example.com
  17. """
  18.  
  19. let activityItemSource = ActivityControllerItemSource(title: "MyApp", text: text)
  20. let activityViewController = UIActivityViewController(
  21. activityItems: [image, activityItemSource],
  22. applicationActivities: nil
  23. )
  24.  
  25. // Required for iPad — present as popover
  26. if let popover = activityViewController.popoverPresentationController,
  27. let rootView = UIApplication.shared.window?.rootViewController?.view {
  28. popover.sourceView = rootView
  29. popover.sourceRect = .zero
  30. popover.permittedArrowDirections = []
  31. }
  32.  
  33. UIApplication.shared.window?.rootViewController?.present(
  34. activityViewController,
  35. animated: true
  36. )
  37. }
  38.  
  39.  
  40. Snapshot extension:
  41.  
  42. import SwiftUI
  43. import UIKit
  44.  
  45. extension View {
  46. #if os(iOS)
  47. var snapshot: UIImage {
  48. let controller = UIHostingController(rootView: self)
  49. let view = controller.view
  50.  
  51. let targetSize = controller.view.intrinsicContentSize
  52. view?.bounds = CGRect(origin: .zero, size: targetSize)
  53. view?.backgroundColor = Color.surface.uiColor
  54.  
  55. let renderer = UIGraphicsImageRenderer(size: targetSize)
  56.  
  57. return renderer.image { _ in
  58. view?.drawHierarchy(in: controller.view.bounds, afterScreenUpdates: true)
  59. }
  60. }
  61. #endif
  62. }
  63.  
  64.  
  65. ActivityControllerItemSource (custom share text + link preview metadata):
  66.  
  67. import UIKit
  68. import LinkPresentation
  69.  
  70. class ActivityControllerItemSource: NSObject, UIActivityItemSource {
  71. var title: String
  72. var text: String
  73.  
  74. init(title: String, text: String) {
  75. self.title = title
  76. self.text = text
  77. super.init()
  78. }
  79.  
  80. func activityViewControllerPlaceholderItem(_ activityViewController: UIActivityViewController) -> Any {
  81. return text
  82. }
  83.  
  84. func activityViewController(_ activityViewController: UIActivityViewController, itemForActivityType activityType: UIActivity.ActivityType?) -> Any? {
  85. return text
  86. }
  87.  
  88. func activityViewController(_ activityViewController: UIActivityViewController, subjectForActivityType activityType: UIActivity.ActivityType?) -> String {
  89. return title
  90. }
  91.  
  92. func activityViewControllerLinkMetadata(_ activityViewController: UIActivityViewController) -> LPLinkMetadata? {
  93. let appIcon = UIImage(named: "AppIconRaw")
  94. let metadata = LPLinkMetadata()
  95. metadata.title = title
  96. metadata.iconProvider = NSItemProvider(object: appIcon ?? UIImage())
  97. metadata.originalURL = URL(fileURLWithPath: text)
  98. return metadata
  99. }
  100. }
  101.  
  102.  
  103. Notes:
  104. - Set an explicit .frame() on the view before calling .snapshot so intrinsicContentSize is correct.
  105. - Uses drawHierarchy(afterScreenUpdates: true) so async layout (images, etc.) has time to render.
  106. - `ActivityControllerItemSource` lets you attach a title, body text, and app icon to the share sheet preview.
  107. - On iPad, UIActivityViewController must be presented as a popover or it will crash.
  108.  
Advertisement
Add Comment
Please, Sign In to add comment