Tests using `usesDrawViewHierarchyInRect` are failing on 26.2 simulators
Using usesDrawViewHierarchyInRect with the iOS 26.2 simulator results in a failing snapshot test with
failed - Snapshot comparison failed: "Images different"
and the following warning in the logs
CGBitmapContextInfoCreate: CGColorSpace which uses extended range requires floating point or CIF10 bitmap context
Demo app: https://github.com/TimofeiKarpovR/SnapshotTesting-color-space-issue-demo/
The failure occurs while creating the image context here: https://github.com/uber/ios-snapshot-test-case/blob/337a423d5245535129af274ad5198a97c4f47271/src/iOSSnapshotTestCaseCore/Categories/UIImage%2BCompare.m#L76
After some investigation:
The reason of the error
drawViewHierarchyInRect now returns an image with an extended color range.
Here is the difference between 26.0 and 26.2 simulators:
| 26.0 | 26.2 | |
|---|---|---|
| Color space | kCGColorSpaceSRGB | kCGColorSpaceExtendedSRGB |
| Float components | NO | YES |
Why imageContext creation fails, but referenceImageContext does not
UIImagePNGRepresentation modifies the original snapshot, removing the extended color range in the process:
| Original | Restored from file | |
|---|---|---|
| Color space | kCGColorSpaceExtendedSRGB | kCGColorSpaceDisplayP3 |
| Float components | YES | NO |
This way, even if we try to support extended color ranges for the contexts in the comparison, they can still fail when comparing the modified reference image with the original one.
Possible solution
Configure the renderer so that it does not use the extended range explicitly.
UIGraphicsImageRendererFormat *format = [UIGraphicsImageRendererFormat preferredFormat];
format.scale = view.window.screen.scale;
format.preferredRange = UIGraphicsImageRendererFormatRangeStandard;
UIGraphicsImageRenderer *graphicsImageRenderer = [[UIGraphicsImageRenderer alloc] initWithSize:bounds.size
format:format];