Skip to content

Commit 7c8b8ff

Browse files
committed
Apply fix from pointfreeco#446
Credit to @dflems
1 parent 32ebb83 commit 7c8b8ff

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

Sources/SnapshotTesting/Snapshotting/UIImage.swift

+16-15
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ extension Diffing where Value == UIImage {
3232
)
3333
}
3434
}
35-
36-
35+
36+
3737
/// Used when the image size has no width or no height to generated the default empty image
3838
private static func emptyImage() -> UIImage {
3939
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 400, height: 80))
@@ -62,6 +62,11 @@ extension Snapshotting where Value == UIImage, Format == UIImage {
6262
}
6363
}
6464

65+
// remap snapshot & reference to same colorspace
66+
let imageContextColorSpace = CGColorSpace(name: CGColorSpace.sRGB)
67+
let imageContextBitsPerComponent = 8
68+
let imageContextBytesPerPixel = 4
69+
6570
private func compare(_ old: UIImage, _ new: UIImage, precision: Float) -> Bool {
6671
guard let oldCgImage = old.cgImage else { return false }
6772
guard let newCgImage = new.cgImage else { return false }
@@ -71,23 +76,18 @@ private func compare(_ old: UIImage, _ new: UIImage, precision: Float) -> Bool {
7176
guard oldCgImage.height != 0 else { return false }
7277
guard newCgImage.height != 0 else { return false }
7378
guard oldCgImage.height == newCgImage.height else { return false }
74-
// Values between images may differ due to padding to multiple of 64 bytes per row,
75-
// because of that a freshly taken view snapshot may differ from one stored as PNG.
76-
// At this point we're sure that size of both images is the same, so we can go with minimal `bytesPerRow` value
77-
// and use it to create contexts.
78-
let minBytesPerRow = min(oldCgImage.bytesPerRow, newCgImage.bytesPerRow)
79-
let byteCount = minBytesPerRow * oldCgImage.height
8079

80+
let byteCount = imageContextBytesPerPixel * oldCgImage.width * oldCgImage.height
8181
var oldBytes = [UInt8](repeating: 0, count: byteCount)
82-
guard let oldContext = context(for: oldCgImage, bytesPerRow: minBytesPerRow, data: &oldBytes) else { return false }
82+
guard let oldContext = context(for: oldCgImage, data: &oldBytes) else { return false }
8383
guard let oldData = oldContext.data else { return false }
84-
if let newContext = context(for: newCgImage, bytesPerRow: minBytesPerRow), let newData = newContext.data {
84+
if let newContext = context(for: newCgImage), let newData = newContext.data {
8585
if memcmp(oldData, newData, byteCount) == 0 { return true }
8686
}
8787
let newer = UIImage(data: new.pngData()!)!
8888
guard let newerCgImage = newer.cgImage else { return false }
8989
var newerBytes = [UInt8](repeating: 0, count: byteCount)
90-
guard let newerContext = context(for: newerCgImage, bytesPerRow: minBytesPerRow, data: &newerBytes) else { return false }
90+
guard let newerContext = context(for: newerCgImage, data: &newerBytes) else { return false }
9191
guard let newerData = newerContext.data else { return false }
9292
if memcmp(oldData, newerData, byteCount) == 0 { return true }
9393
if precision >= 1 { return false }
@@ -100,16 +100,17 @@ private func compare(_ old: UIImage, _ new: UIImage, precision: Float) -> Bool {
100100
return true
101101
}
102102

103-
private func context(for cgImage: CGImage, bytesPerRow: Int, data: UnsafeMutableRawPointer? = nil) -> CGContext? {
103+
private func context(for cgImage: CGImage, data: UnsafeMutableRawPointer? = nil) -> CGContext? {
104+
let bytesPerRow = cgImage.width * imageContextBytesPerPixel
104105
guard
105-
let space = cgImage.colorSpace,
106+
let colorSpace = imageContextColorSpace,
106107
let context = CGContext(
107108
data: data,
108109
width: cgImage.width,
109110
height: cgImage.height,
110-
bitsPerComponent: cgImage.bitsPerComponent,
111+
bitsPerComponent: imageContextBitsPerComponent,
111112
bytesPerRow: bytesPerRow,
112-
space: space,
113+
space: colorSpace,
113114
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue
114115
)
115116
else { return nil }

0 commit comments

Comments
 (0)