Skip to content

Commit

Permalink
Set maxSize for preview image
Browse files Browse the repository at this point in the history
  • Loading branch information
ra1028 committed Feb 13, 2024
1 parent c4d8b9d commit bab6ae4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
16 changes: 13 additions & 3 deletions Sources/Playbook/SnapshotSupport/Internal/SnapshotWindow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ internal final class SnapshotWindow: UIWindow {
private let scenario: Scenario
private let device: SnapshotDevice
private let scenarioViewController: ScenarioViewController
private let maxSize: CGSize?

var contentView: UIView? {
scenarioViewController.contentViewController?.view
Expand All @@ -24,7 +25,11 @@ internal final class SnapshotWindow: UIWindow {
return safeAreaInsets
}

init(scenario: Scenario, device: SnapshotDevice) {
init(
scenario: Scenario,
device: SnapshotDevice,
maxSize: CGSize? = nil
) {
let context = ScenarioContext(
snapshotWaiter: SnapshotWaiter(),
isSnapshot: true,
Expand All @@ -33,6 +38,7 @@ internal final class SnapshotWindow: UIWindow {

self.scenario = scenario
self.device = device
self.maxSize = maxSize
self.scenarioViewController = ScenarioViewController(context: context, scenario: scenario)

super.init(frame: .zero)
Expand All @@ -51,9 +57,13 @@ internal final class SnapshotWindow: UIWindow {
layer.speed = .greatestFiniteMagnitude
rootViewController = scenarioViewController
isHidden = false

let idealWidth = scenario.layout.fixedWidth ?? device.size.width
let idealHeight = scenario.layout.fixedHeight ?? device.size.height

frame.size = CGSize(
width: scenario.layout.fixedWidth ?? device.size.width,
height: scenario.layout.fixedHeight ?? device.size.height
width: maxSize.map { min($0.width, idealWidth) } ?? idealWidth,
height: maxSize.map { min($0.height, idealHeight) } ?? idealHeight
)

if window != nil {
Expand Down
31 changes: 14 additions & 17 deletions Sources/Playbook/SnapshotSupport/SnapshotSupport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public enum SnapshotSupport {
for scenario: Scenario,
on device: SnapshotDevice,
format: ImageFormat,
maxSize: CGSize? = nil,
scale: CGFloat = UIScreen.main.scale,
keyWindow: UIWindow? = nil,
viewPreprocessor: ((UIView) -> UIView)? = nil,
Expand All @@ -48,6 +49,7 @@ public enum SnapshotSupport {
makeResource(
for: scenario,
on: device,
maxSize: maxSize,
scale: scale,
keyWindow: keyWindow,
viewPreprocessor: viewPreprocessor
Expand All @@ -73,13 +75,15 @@ public enum SnapshotSupport {
for scenario: Scenario,
on device: SnapshotDevice,
scale: CGFloat = UIScreen.main.scale,
maxSize: CGSize? = nil,
keyWindow: UIWindow? = nil,
viewPreprocessor: ((UIView) -> UIView)? = nil,
handler: @escaping (UIImage) -> Void
) {
makeResource(
for: scenario,
on: device,
maxSize: maxSize,
scale: scale,
keyWindow: keyWindow,
viewPreprocessor: viewPreprocessor
Expand All @@ -98,15 +102,15 @@ private extension SnapshotSupport {
static func makeResource(
for scenario: Scenario,
on device: SnapshotDevice,
maxSize: CGSize?,
scale: CGFloat,
keyWindow: UIWindow?,
viewPreprocessor: ((UIView) -> UIView)? = nil,
completion: @escaping (Resource) -> Void
) {
withoutAnimation {
let window = SnapshotWindow(scenario: scenario, device: device)
let window = SnapshotWindow(scenario: scenario, device: device, maxSize: maxSize)
let contentView = window.contentView!

let isEmbedInKeyWindow: Bool

if let keyWindow = keyWindow {
Expand All @@ -128,22 +132,15 @@ private extension SnapshotSupport {

let format = UIGraphicsImageRendererFormat(for: device.traitCollection)
format.scale = scale
format.preferredRange = .standard

if #available(iOS 12.0, *) {
format.preferredRange = .standard
}
else {
format.prefersExtendedRange = false
}

var snapshotView: UIView

if let viewPreprocessor = viewPreprocessor {
snapshotView = viewPreprocessor(contentView)
}
else {
snapshotView = contentView
}
let snapshotView =
if let viewPreprocessor = viewPreprocessor {
viewPreprocessor(contentView)
}
else {
contentView
}

let renderer = UIGraphicsImageRenderer(bounds: snapshotView.bounds, format: format)
let actions: UIGraphicsDrawingActions = { context in
Expand Down
1 change: 1 addition & 0 deletions Sources/PlaybookUI/Internal/Utilities/ImageLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ private extension ImageLoader {
)
),
format: .png,
maxSize: item.source.size,
scale: item.source.scale
) { [weak self] data in
guard let self else {
Expand Down

0 comments on commit bab6ae4

Please sign in to comment.