mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
7f2dd1d49c
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
38 lines
1.4 KiB
Objective-C
38 lines
1.4 KiB
Objective-C
/*
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
#import "UIImage+Diff.h"
|
|
|
|
@implementation UIImage (Diff)
|
|
|
|
- (UIImage *)diffWithImage:(UIImage *)image
|
|
{
|
|
if (!image) {
|
|
return nil;
|
|
}
|
|
CGSize imageSize = CGSizeMake(MAX(self.size.width, image.size.width), MAX(self.size.height, image.size.height));
|
|
|
|
UIGraphicsImageRendererFormat *const rendererFormat = [UIGraphicsImageRendererFormat defaultFormat];
|
|
rendererFormat.opaque = YES;
|
|
UIGraphicsImageRenderer *const renderer = [[UIGraphicsImageRenderer alloc] initWithSize:imageSize
|
|
format:rendererFormat];
|
|
|
|
return [renderer imageWithActions:^(UIGraphicsImageRendererContext *_Nonnull rendererContext) {
|
|
const CGContextRef context = rendererContext.CGContext;
|
|
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
|
|
CGContextSetAlpha(context, 0.5f);
|
|
CGContextBeginTransparencyLayer(context, NULL);
|
|
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
|
|
CGContextSetBlendMode(context, kCGBlendModeDifference);
|
|
CGContextSetFillColorWithColor(context, [UIColor whiteColor].CGColor);
|
|
CGContextFillRect(context, CGRectMake(0, 0, self.size.width, self.size.height));
|
|
CGContextEndTransparencyLayer(context);
|
|
}];
|
|
}
|
|
|
|
@end
|