diff --git a/CHANGELOG.md b/CHANGELOG.md index ccd136015..991ab3147 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## Next +- recording: improve ios session recording performance by avoiding redrawing after screen updates ([#166](https://github.com/PostHog/posthog-ios/pull/166)) + - `debouncerDelay` is changed from 500ms to 1s since the iOS screenshot has to be taken in the main thread and its more sensitive to performance issues + ## 3.7.1 - 2024-08-13 - recording: improve ios session recording performance by doing some work off of the main thread ([#158](https://github.com/PostHog/posthog-ios/pull/158)) diff --git a/PostHog/Replay/PostHogReplayIntegration.swift b/PostHog/Replay/PostHogReplayIntegration.swift index e5c2c56af..64f63a355 100644 --- a/PostHog/Replay/PostHogReplayIntegration.swift +++ b/PostHog/Replay/PostHogReplayIntegration.swift @@ -460,9 +460,6 @@ } @objc private func snapshot() { - // TODO: add debouncer with debouncerDelayMs to take into account how long it takes to execute the - // snapshot method - if !PostHogSDK.shared.isSessionReplayActive() { return } diff --git a/PostHog/Replay/PostHogSessionReplayConfig.swift b/PostHog/Replay/PostHogSessionReplayConfig.swift index 6eb2a8e4c..b8edc8ed8 100644 --- a/PostHog/Replay/PostHogSessionReplayConfig.swift +++ b/PostHog/Replay/PostHogSessionReplayConfig.swift @@ -33,8 +33,8 @@ /// Deboucer delay used to reduce the number of snapshots captured and reduce performance impact /// This is used for capturing the view as a wireframe or screenshot /// The lower the number more snapshots will be captured but higher the performance impact - /// Defaults to 500ms - @objc public var debouncerDelay: TimeInterval = 1.0 / 2.0 + /// Defaults to 1s + @objc public var debouncerDelay: TimeInterval = 1.0 // TODO: sessionRecording config such as networkPayloadCapture, captureConsoleLogs, sampleRate, etc } diff --git a/PostHog/Replay/UIView+Util.swift b/PostHog/Replay/UIView+Util.swift index fc45e4f36..74724b389 100644 --- a/PostHog/Replay/UIView+Util.swift +++ b/PostHog/Replay/UIView+Util.swift @@ -34,17 +34,25 @@ } func toImage() -> UIImage? { - let size = bounds.size + // Avoid Rendering Offscreen Views + let bounds = superview?.bounds ?? bounds + let size = bounds.intersection(bounds).size if !size.hasSize() { return nil } - let renderer = UIGraphicsImageRenderer(size: size) + let rendererFormat = UIGraphicsImageRendererFormat.default() - let image = renderer.image { context in - // Render the view's layer into the current context - layer.render(in: context.cgContext) + // This can significantly improve rendering performance because the renderer won't need to + // process transparency. + rendererFormat.opaque = isOpaque + // Another way to improve rendering performance is to scale the renderer's content. + // rendererFormat.scale = 0.5 + let renderer = UIGraphicsImageRenderer(size: size, format: rendererFormat) + + let image = renderer.image { _ in + drawHierarchy(in: bounds, afterScreenUpdates: false) } return image