Skip to content

fix(png): Preserve color and alpha in extended-range snapshots - #284

Merged
jamieQ merged 5 commits into
mainfrom
fix/png-alpha-export
Oct 8, 2026
Merged

jamieQ merged 5 commits into
mainfrom
fix/png-alpha-export

Conversation

@jamieQ

@jamieQ jamieQ commented Oct 7, 2026 •

Copy link
Copy Markdown
Member

As reported in #281, there seems to be some odd interactions when rendering a UIImage via UIGraphicsImageRenderer and then exporting it to PNG via its pngData() method. Specifically, if the renderer produces a 16-bit image, which UIKit may do in some cases, then the encoded PNG can include color values that are premultiplied by alpha, which violates the PNG spec. This results in exporting snapshot image that may not match the manner in which the "real" preview images are displayed. Work around this by using ImageIO to encode the PNG data, which appears to avoid the issue.

Stacked on #283, which ensures our unit tests run in CI.

Refs EME-1331

@jamieQ
jamieQ added this pull request to stack #285 October 8, 2026 00:05
@sentry

sentry Bot commented Oct 8, 2026 •

Copy link
Copy Markdown

Sentry Snapshot Testing

Name Added Removed Changed Renamed Unchanged Skipped Status
com.example.multimoduledemo
com.example.multimoduledemo
0 0 1 0 8 0 ✅ Approved

⚙️ snapshot-previews Snapshot Settings

@jamieQ
jamieQ marked this pull request as ready for review October 8, 2026 09:52
Base automatically changed from ci/run-ios-package-tests to main October 8, 2026 14:26
pixelmatrix and others added 3 commits October 8, 2026 09:26
Preserve UIImage orientation metadata when exporting through ImageIO. Use small color-aware regression fixtures and a strict expected-failure check to flag a future UIKit alpha fix.

Refs #281
@jamieQ
jamieQ force-pushed the fix/png-alpha-export branch from 037f71a to 5c5ba3f Compare October 8, 2026 14:26
return try XCTUnwrap(CGImageSourceCopyPropertiesAtIndex(source, 0, nil) as? [CFString: Any])
}

private func pngPixel(_ data: Data) throws -> [UInt16] {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only one thing. i had an agent look at these tests and it called the following out:

  • pngPixel in UIImagePNGEncodingTests.swift assumes a 16-bit PNG, but the bit depth depends on the simulator runtime. Extended-range sources come out 8-bit Display P3 on iOS 17.5 and 26.0, and 16-bit sRGB on 18.0. testExtendedRangePNGPreservesColorAndAlpha and testUIKitPNGEncodingStillRequiresAlphaWorkaround fail on 17.5, 18.5 and 26.0. CI is green only because of the runtime it currently picks, and unit-tests.yml doesn't pin Xcode or the simulator OS.
  • Suggested fix: drop pngPixel and assert through assertEqualPixels(decode(...), image.cgImage) instead, which removes the bit-depth dependence, import zlib and the colour-space conversion block. Needs a check that this still fails against image.pngData().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I think it will simplify the tests nicely.

Compare decoded pixels with the source image instead of parsing 16-bit PNG samples.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 372eb15. Configure here.

Comment thread Tests/SnapshotPreviewsTests/UIImagePNGEncodingTests.swift
@jamieQ
jamieQ merged commit e90a099 into main Oct 8, 2026
30 checks passed
@jamieQ
jamieQ deleted the fix/png-alpha-export branch October 8, 2026 19:14
@linear-code

linear-code Bot commented Oct 8, 2026

Copy link
Copy Markdown

EME-1331

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants