Pull out CGContext early in UIImage+Diff (#35940)

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 https://github.com/microsoft/react-native-macos/issues/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: https://github.com/facebook/react-native/pull/35940

Test Plan: Build should pass.

Reviewed By: matrush

Differential Revision: D42761424

Pulled By: javache

fbshipit-source-id: 5ce79a5e7c60484dd37d0b2c3f58ff0897d662df
This commit is contained in:
Saad Najmi authored and Facebook GitHub Bot committed 2023-02-05 06:49:08 -08:00
1 parent 11ece22fc6
commit 7f2dd1d49c
1 file changed
+8 -7
@@ -21,15 +21,16 @@
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);
}];
}