Skip to content

Commit 8cb35f7

Browse files
committed
Added support for SwiftUI's Image.
1 parent 6e68c00 commit 8cb35f7

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

README.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,34 +73,33 @@ present(activityImageVC, animated: true)
7373
import SwiftUI
7474
import PSActivityImageViewController
7575

76-
struct MyView: View {
77-
78-
let image: Image
76+
struct ContentView: View {
77+
78+
let image = Image("Image")
7979

8080
@State
81-
var activityItem: ActivityItem? = nil
81+
var activityItem: ActivityImageItem? = nil
8282

8383
var body: some View {
8484

8585
VStack(spacing: 16) {
86-
86+
8787
image
88+
.resizable()
89+
.aspectRatio(contentMode: .fit)
90+
.padding()
8891

8992
Button(
9093
action: {
91-
activityItem = ActivityImageItem(
92-
image: image,
93-
activities: image
94-
)
94+
activityItem = ActivityImageItem(image: image)
9595
},
9696
label: {
9797
Text("Share image")
9898
}
9999
)
100-
100+
.activityImageSheet($activityItem)
101101
}
102102
.padding()
103-
.activityImageSheet($activityItem)
104103
}
105104
}
106105
```

Sources/PSActivityImageViewController/SwiftUI/ActivityImageView.swift

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,13 @@ private final class ActivityViewControllerWrapper: UIViewController {
122122
return
123123
}
124124

125+
let uiImage = item.image.snapshot()
126+
var completeItems: [Any] = [uiImage]
127+
completeItems.append(contentsOf: item.items)
128+
125129
let controller = ActivityImageViewController(
126-
image: item.image,
127-
activityItems: item.items,
130+
image: uiImage,
131+
activityItems: completeItems,
128132
activities: item.activities,
129133
excludedTypes: item.excludedTypes,
130134
completion: { [weak self] activityType, success, items, error in
@@ -143,5 +147,30 @@ private final class ActivityViewControllerWrapper: UIViewController {
143147
}
144148
}
145149

150+
@available(iOS 13, *)
151+
extension View {
152+
153+
func snapshot() -> UIImage {
154+
155+
let controller = UIHostingController(rootView: self)
156+
let view = controller.view
157+
158+
let targetSize = controller.view.intrinsicContentSize
159+
160+
view?.bounds = CGRect(origin: .zero, size: targetSize)
161+
view?.backgroundColor = .clear
162+
163+
let renderer = UIGraphicsImageRenderer(size: targetSize)
164+
165+
return renderer.image { _ in
166+
167+
view?.drawHierarchy(
168+
in: controller.view.bounds,
169+
afterScreenUpdates: true
170+
)
171+
}
172+
}
173+
}
174+
146175
#endif
147176

Sources/PSActivityImageViewController/SwiftUI/ActivityItem.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,23 @@
55
// Created by Peter Salz on 08.09.21.
66
//
77

8+
#if canImport(SwiftUI)
9+
810
import UIKit
11+
import SwiftUI
912

1013
/// Represents an activity for presenting an `ActivityView` (share sheet) via the `activitySheet`
1114
/// modifier.
1215
@available(iOS 13, *)
1316
public struct ActivityImageItem {
1417

15-
internal var image: UIImage
18+
internal var image: Image
1619
internal var items: [Any]
1720
internal var activities: [UIActivity]
1821
internal var excludedTypes: [UIActivity.ActivityType]
1922

2023
public init(
21-
image: UIImage,
24+
image: Image,
2225
items: Any...,
2326
activities: [UIActivity] = [],
2427
excludedTypes: [UIActivity.ActivityType] = []
@@ -30,3 +33,5 @@ public struct ActivityImageItem {
3033
self.excludedTypes = excludedTypes
3134
}
3235
}
36+
37+
#endif

0 commit comments

Comments
 (0)