Skip to content

Commit

Permalink
Pull out CGContext early in UIImage+Diff (#35940)
Browse files Browse the repository at this point in the history
Summary:
This is a small refactor designed to make future merges for React Native macOS easier. It was found while merging React Native 0.71 into React Native macOS.

In React Native macOS, we ifdef iOS  out API's and replace them with macOS APIs, reusing as much code as possible. `CGContext` is a type that is available on both iOS and macOS, but `UIGraphicsImageRendererContext` is not. A simple refactor makes it easier to reuse this code in React Native macOS without affecting iOS :)

Resolves microsoft#1676

## Changelog

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[IOS] [CHANGED] - Pull out CGContext early in UIImage+Diff

Pull Request resolved: #35940

Test Plan: Build should pass.

Reviewed By: matrush

Differential Revision: D42761424

Pulled By: javache

fbshipit-source-id: 5ce79a5e7c60484dd37d0b2c3f58ff0897d662df
  • Loading branch information
Saadnajmi authored and facebook-github-bot committed Feb 5, 2023
1 parent 11ece22 commit 7f2dd1d
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions packages/rn-tester/RCTTest/FBSnapshotTestCase/UIImage+Diff.m
Original file line number Diff line number Diff line change
@@ -21,15 +21,16 @@ - (UIImage *)diffWithImage:(UIImage *)image
UIGraphicsImageRenderer *const renderer = [[UIGraphicsImageRenderer alloc] initWithSize:imageSize
format:rendererFormat];

return [renderer imageWithActions:^(UIGraphicsImageRendererContext *_Nonnull context) {
return [renderer imageWithActions:^(UIGraphicsImageRendererContext *_Nonnull rendererContext) {
const CGContextRef context = rendererContext.CGContext;
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
CGContextSetAlpha(context.CGContext, 0.5f);
CGContextBeginTransparencyLayer(context.CGContext, NULL);
CGContextSetAlpha(context, 0.5f);
CGContextBeginTransparencyLayer(context, NULL);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
CGContextSetBlendMode(context.CGContext, kCGBlendModeDifference);
CGContextSetFillColorWithColor(context.CGContext, [UIColor whiteColor].CGColor);
CGContextFillRect(context.CGContext, CGRectMake(0, 0, self.size.width, self.size.height));
CGContextEndTransparencyLayer(context.CGContext);
CGContextSetBlendMode(context, kCGBlendModeDifference);
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
CGContextFillRect(context, CGRectMake(0, 0, self.size.width, self.size.height));
CGContextEndTransparencyLayer(context);
}];
}

0 comments on commit 7f2dd1d

Please sign in to comment.