|
96 | 96 | windowViews.setObject(snapshotStatus, forKey: view)
|
97 | 97 | }
|
98 | 98 |
|
| 99 | + // TODO: IncrementalSnapshot, type=2 |
| 100 | + |
99 | 101 | var wireframes: [Any] = []
|
100 | 102 | wireframes.append(wireframe.toDict())
|
101 | 103 | let initialOffset = ["top": 0, "left": 0]
|
|
141 | 143 | wireframe.height = Int(view.frame.size.height)
|
142 | 144 | let style = RRStyle()
|
143 | 145 |
|
| 146 | + // no parent id means its the root |
| 147 | + if parentId == nil, config.sessionReplayConfig.screenhshotMode { |
| 148 | + if let image = view.toImage() { |
| 149 | + wireframe.base64 = imageToBase64(image) |
| 150 | + } |
| 151 | + wireframe.type = "screenshot" |
| 152 | + return wireframe |
| 153 | + } |
| 154 | + |
144 | 155 | if let textView = view as? UITextView {
|
145 | 156 | wireframe.type = "text"
|
146 | 157 | let isSensitive = config.sessionReplayConfig.maskAllTextInputs || textView.isNoCapture() || textView.isSensitiveText()
|
|
190 | 201 | if let image = view as? UIImageView {
|
191 | 202 | wireframe.type = "image"
|
192 | 203 | if !image.isNoCapture(), !config.sessionReplayConfig.maskAllImages {
|
193 |
| - // TODO: check png quality |
194 |
| - wireframe.base64 = image.image?.pngData()?.base64EncodedString() |
| 204 | + if let image = image.image { |
| 205 | + wireframe.base64 = imageToBase64(image) |
| 206 | + } |
195 | 207 | }
|
196 | 208 | }
|
197 | 209 |
|
|
274 | 286 | }
|
275 | 287 |
|
276 | 288 | @objc private func snapshot() {
|
| 289 | + // TODO: add debouncer with debouncerDelayMs to take into account how long it takes to execute the |
| 290 | + // snapshot method |
| 291 | + |
277 | 292 | if !PostHogSDK.shared.isSessionReplayActive() {
|
278 | 293 | return
|
279 | 294 | }
|
|
289 | 304 |
|
290 | 305 | var screenName: String?
|
291 | 306 | if let controller = window.rootViewController {
|
292 |
| - if controller is AnyObjectUIHostingViewController { |
293 |
| - hedgeLog("SwiftUI snapshot not supported.") |
| 307 | + // SwiftUI only supported with screenshot |
| 308 | + if controller is AnyObjectUIHostingViewController, !config.sessionReplayConfig.screenhshotMode { |
| 309 | + hedgeLog("SwiftUI snapshot not supported, enable screenshot mode.") |
294 | 310 | return
|
| 311 | + // screen name only makes sense if we are not using SwiftUI |
| 312 | + } else if !config.sessionReplayConfig.screenhshotMode { |
| 313 | + screenName = UIViewController.getViewControllerName(controller) |
295 | 314 | }
|
296 |
| - screenName = UIViewController.getViewControllerName(controller) |
297 | 315 | }
|
298 | 316 |
|
299 | 317 | // this cannot run off of the main thread because most properties require to be called within the main thread
|
300 | 318 | // this method has to be fast and do as little as possible
|
301 | 319 | generateSnapshot(window, screenName)
|
302 | 320 | }
|
| 321 | + |
| 322 | + private func imageToBase64(_ image: UIImage) -> String? { |
| 323 | + let jpegData = image.jpegData(compressionQuality: 0.3) |
| 324 | + let base64 = jpegData?.base64EncodedString() |
| 325 | + |
| 326 | + if let base64 = base64 { |
| 327 | + return "data:image/jpeg;base64,\(base64)" |
| 328 | + } |
| 329 | + |
| 330 | + return nil |
| 331 | + } |
303 | 332 | }
|
304 | 333 |
|
305 | 334 | private protocol AnyObjectUIHostingViewController: AnyObject {}
|
|
0 commit comments