Skip to content

Commit 29d6bcc

Browse files
committed
Capture pixel screenshots implementation on iOS
1 parent 30539ad commit 29d6bcc

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

platform/swift/source/replay/SessionReplayTarget.swift

+24-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
@_implementationOnly import CapturePassable
99
import Foundation
10+
import UIKit
1011

1112
final class SessionReplayTarget {
1213
private let queue = DispatchQueue.serial(withLabelSuffix: "ReplayController", target: .default)
@@ -41,14 +42,28 @@ extension SessionReplayTarget: CapturePassable.SessionReplayTarget {
4142
}
4243

4344
func captureScreenshot() {
44-
// TODO: Implement
45-
// DispatchQueue.main.async { [weak self] in
46-
// self?.queue.async {
47-
// self?.logger?.logSessionReplayScreenshot(
48-
// screen: SessionReplayCapture(data: Data()),
49-
// duration: 0
50-
// )
51-
// }
52-
// }
45+
DispatchQueue.main.async {
46+
guard let window = UIApplication.shared.sessionReplayWindows().first else {
47+
return
48+
}
49+
50+
let layer = window.layer
51+
let bounds = UIScreen.main.bounds.size
52+
53+
self.queue.async { [weak self] in
54+
let start = Uptime()
55+
let format = UIGraphicsImageRendererFormat()
56+
format.scale = 1.0
57+
58+
let renderer = UIGraphicsImageRenderer(size: bounds, format: format)
59+
let jpeg = renderer.jpegData(withCompressionQuality: 0.1) { context in
60+
layer.render(in: context.cgContext)
61+
}
62+
self?.logger?.logSessionReplayScreenshot(
63+
screen: SessionReplayCapture(data: jpeg),
64+
duration: Uptime().timeIntervalSince(start)
65+
)
66+
}
67+
}
5368
}
5469
}

0 commit comments

Comments
 (0)