Skip to content

Commit

Permalink
Add workaround for the scenarios that has size over the internal size…
Browse files Browse the repository at this point in the history
… limitation 8192x8192 (#66)
  • Loading branch information
ra1028 authored Feb 16, 2024
1 parent 0a18e71 commit 798f686
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
26 changes: 25 additions & 1 deletion Sources/Playbook/SnapshotSupport/SnapshotSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,31 @@ private extension SnapshotSupport {
let actions: UIGraphicsDrawingActions = { context in
withoutAnimation {
if isEmbedInKeyWindow {
snapshotView.drawHierarchy(in: snapshotView.bounds, afterScreenUpdates: true)
let nativeScale = UIScreen.main.nativeScale
let size = snapshotView.bounds.size
let nativeScaledSize = CGSize(
width: size.width * nativeScale,
height: size.height * nativeScale
)
let sizeLimit: CGFloat = 8192
let limitedScale = max(nativeScaledSize.width / sizeLimit, nativeScaledSize.height / sizeLimit)

if limitedScale > 1 {
// `UIView.drawHierarchy` seems to have an internal hard-coded limit size of 8192x8192,
// and specifying a size beyond that resulting in an empty image is drawn.
let limitedRect = CGRect(
x: .zero,
y: .zero,
width: size.width / limitedScale,
height: size.height / limitedScale
)
context.cgContext.scaleBy(x: limitedScale, y: limitedScale)
snapshotView.drawHierarchy(in: limitedRect, afterScreenUpdates: true)
}
else {
snapshotView.drawHierarchy(in: snapshotView.bounds, afterScreenUpdates: true)
}

snapshotView.removeFromSuperview()
}
else {
Expand Down
8 changes: 7 additions & 1 deletion Tests/ExtraScenarios.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ import SwiftUI

enum ExtraScenarios: ScenarioProvider {
static func addScenarios(into playbook: Playbook) {
playbook.addScenarios(of: "Extra") {
Scenario("Over internal size limitation 8192x8192", layout: .fixed(width: 10000, height: 10000)) {
Color.red.edgesIgnoringSafeArea(.all)
}
}

playbook.addScenarios(of: "Normalizable\u{7FFFF}\u{1D} \n.:/") {
Scenario("Normalizable\u{7FFFF}\u{1D} \n.:/", layout: .fixed(length: 300)) {
Color(.systemBlue).edgesIgnoringSafeArea(.all)
Color.blue.edgesIgnoringSafeArea(.all)
}
}
}
Expand Down

0 comments on commit 798f686

Please sign in to comment.