Skip to content

Commit 5406504

Browse files
authored
refactor: introduce RCTPlatformImage (1/2) (#2783)
## Summary: This PR is split out from #2766 to help make it smaller. Previously we had the code `@compatibility_alias UIImage NSImage` which is basically an objective C runtime way of doing `#define UIImage NSImage`. We don't quite like this, since we are defining the symbol `UIImage` in an Appkit context. We have a better pattern we can copy from RCTPlatformView --> RCTPlatformImage. The `RCTPlatform` prefix basically means it's NS<blah> on Appkit and UI<blah> on UIKit. This is as opposed to something like RCTUIView, where on iOS it's a typedef, but on macOS it's a subclass that adds stuff to NSView to make it more compatible with UIView. All in all, this should mostly be a rigorous find/replace to make the code slightly more strict, but have no behavioral changes ## Test Plan: RNTester runs locally.
1 parent 8c1b168 commit 5406504

39 files changed

Lines changed: 115 additions & 113 deletions

packages/react-native/Libraries/Image/RCTAnimatedImage.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111
@property (nonatomic, assign, readonly) NSUInteger animatedImageFrameCount;
1212
@property (nonatomic, assign, readonly) NSUInteger animatedImageLoopCount;
1313

14-
- (nullable UIImage *)animatedImageFrameAtIndex:(NSUInteger)index;
14+
- (nullable RCTPlatformImage *)animatedImageFrameAtIndex:(NSUInteger)index; // [macOS]
1515
- (NSTimeInterval)animatedImageDurationAtIndex:(NSUInteger)index;
1616

1717
@end
1818

19-
@interface RCTAnimatedImage : UIImage <RCTAnimatedImage>
19+
@interface RCTAnimatedImage : RCTPlatformImage <RCTAnimatedImage> // [macOS]
2020
// [macOS
2121
// This is a known initializer for UIImage, but needs to be exposed publicly for macOS since
2222
// this is not a known initializer for NSImage

packages/react-native/Libraries/Image/RCTAnimatedImage.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ - (NSTimeInterval)animatedImageDurationAtIndex:(NSUInteger)index
148148
return _frames[index].duration;
149149
}
150150

151-
- (UIImage *)animatedImageFrameAtIndex:(NSUInteger)index
151+
- (RCTPlatformImage *)animatedImageFrameAtIndex:(NSUInteger)index // [macOS]
152152
{
153153
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(_imageSource, index, NULL);
154154
if (!imageRef) {
@@ -157,7 +157,7 @@ - (UIImage *)animatedImageFrameAtIndex:(NSUInteger)index
157157
#if !TARGET_OS_OSX // [macOS]
158158
UIImage *image = [[UIImage alloc] initWithCGImage:imageRef scale:_scale orientation:UIImageOrientationUp];
159159
#else // [macOS
160-
UIImage *image = [[NSImage alloc] initWithCGImage:imageRef size:CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef))];
160+
NSImage *image = [[NSImage alloc] initWithCGImage:imageRef size:CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef))];
161161
#endif // macOS]
162162
CGImageRelease(imageRef);
163163
return image;

packages/react-native/Libraries/Image/RCTBundleAssetImageLoader.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ - (nullable RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL
4949
partialLoadHandler:(RCTImageLoaderPartialLoadBlock)partialLoadHandler
5050
completionHandler:(RCTImageLoaderCompletionBlock)completionHandler
5151
{
52-
UIImage *image = RCTImageFromLocalAssetURL(imageURL);
52+
RCTPlatformImage *image = RCTImageFromLocalAssetURL(imageURL); // [macOS]
5353
if (image) {
5454
if (progressHandler) {
5555
progressHandler(1, 1);

packages/react-native/Libraries/Image/RCTImageBlurUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@
1010

1111
#import <React/RCTDefines.h>
1212

13-
RCT_EXTERN UIImage *RCTBlurredImageWithRadius(UIImage *inputImage, CGFloat radius);
13+
RCT_EXTERN RCTPlatformImage *RCTBlurredImageWithRadius(RCTPlatformImage *inputImage, CGFloat radius); // [macOS]

packages/react-native/Libraries/Image/RCTImageBlurUtils.mm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#import <React/RCTUIKit.h> // [macOS]
1111
#import <React/RCTUtils.h> // [macOS]
1212

13-
UIImage *RCTBlurredImageWithRadius(UIImage *inputImage, CGFloat radius)
13+
RCTPlatformImage *RCTBlurredImageWithRadius(RCTPlatformImage *inputImage, CGFloat radius) // [macOS]
1414
{
1515
CGImageRef imageRef = UIImageGetCGImageRef(inputImage); // [macOS]
1616
CGFloat imageScale = UIImageGetScale(inputImage); // [macOS]

packages/react-native/Libraries/Image/RCTImageCache.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
#import <React/RCTResizeMode.h>
1212

13-
@interface UIImage (React)
13+
@interface RCTPlatformImage (React) // [macOS]
1414

1515
/**
1616
* Memory bytes of the image with the default calculation of static image or GIF. Custom calculations of decoded bytes
@@ -25,9 +25,9 @@
2525
*/
2626
@protocol RCTImageCache <NSObject>
2727

28-
- (UIImage *)imageForUrl:(NSString *)url size:(CGSize)size scale:(CGFloat)scale resizeMode:(RCTResizeMode)resizeMode;
28+
- (RCTPlatformImage *)imageForUrl:(NSString *)url size:(CGSize)size scale:(CGFloat)scale resizeMode:(RCTResizeMode)resizeMode; // [macOS]
2929

30-
- (void)addImageToCache:(UIImage *)image
30+
- (void)addImageToCache:(RCTPlatformImage *)image // [macOS]
3131
URL:(NSString *)url
3232
size:(CGSize)size
3333
scale:(CGFloat)scale

packages/react-native/Libraries/Image/RCTImageCache.mm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ - (void)clearCache
7171
}
7272
#endif // [macOS]
7373

74-
- (void)addImageToCache:(UIImage *)image forKey:(NSString *)cacheKey
74+
- (void)addImageToCache:(RCTPlatformImage *)image forKey:(NSString *)cacheKey // [macOS]
7575
{
7676
if (!image) {
7777
return;
@@ -82,7 +82,7 @@ - (void)addImageToCache:(UIImage *)image forKey:(NSString *)cacheKey
8282
}
8383
}
8484

85-
- (UIImage *)imageForUrl:(NSString *)url size:(CGSize)size scale:(CGFloat)scale resizeMode:(RCTResizeMode)resizeMode
85+
- (RCTPlatformImage *)imageForUrl:(NSString *)url size:(CGSize)size scale:(CGFloat)scale resizeMode:(RCTResizeMode)resizeMode // [macOS]
8686
{
8787
NSString *cacheKey = RCTCacheKeyForImage(url, size, scale, resizeMode);
8888
@synchronized(_cacheStaleTimes) {
@@ -99,7 +99,7 @@ - (UIImage *)imageForUrl:(NSString *)url size:(CGSize)size scale:(CGFloat)scale
9999
return [_decodedImageCache objectForKey:cacheKey];
100100
}
101101

102-
- (void)addImageToCache:(UIImage *)image
102+
- (void)addImageToCache:(RCTPlatformImage *)image // [macOS]
103103
URL:(NSString *)url
104104
size:(CGSize)size
105105
scale:(CGFloat)scale

packages/react-native/Libraries/Image/RCTImageEditingManager.mm

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ @implementation RCTImageEditingManager
6060

6161
[[_moduleRegistry moduleForName:"ImageLoader"]
6262
loadImageWithURLRequest:imageRequest
63-
callback:^(NSError *error, UIImage *image) {
63+
callback:^(NSError *error, RCTPlatformImage *image) { // [macOS]
6464
if (error) {
6565
errorCallback(@[ RCTJSErrorFromNSError(error) ]);
6666
return;
@@ -70,7 +70,7 @@ @implementation RCTImageEditingManager
7070
CGSize targetSize = rect.size;
7171
CGRect targetRect = {{-rect.origin.x, -rect.origin.y}, image.size};
7272
CGAffineTransform transform = RCTTransformFromTargetRect(image.size, targetRect);
73-
UIImage *croppedImage = RCTTransformImage(image, targetSize, UIImageGetScale(image), transform); // [macOS]
73+
RCTPlatformImage *croppedImage = RCTTransformImage(image, targetSize, UIImageGetScale(image), transform); // [macOS]
7474

7575
// Scale image
7676
if (cropDataCopy.displaySize()) {

packages/react-native/Libraries/Image/RCTImageLoader.mm

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626

2727
using namespace facebook::react;
2828

29-
static NSInteger RCTImageBytesForImage(UIImage *image)
29+
static NSInteger RCTImageBytesForImage(RCTPlatformImage *image) // [macOS]
3030
{
3131
CGFloat imageScale = 1.0;
3232
#if !TARGET_OS_OSX // [macOS] no .scale prop on NSImage
@@ -89,7 +89,7 @@ @interface RCTImageLoader () <NativeImageLoaderIOSSpec, RCTImageLoaderWithAttrib
8989

9090
@end
9191

92-
@implementation UIImage (React)
92+
@implementation RCTPlatformImage (React) // [macOS]
9393

9494
- (NSInteger)reactDecodedImageBytes
9595
{
@@ -345,7 +345,7 @@ - (void)setImageCache:(id<RCTImageCache>)cache
345345
return nil;
346346
}
347347

348-
static UIImage *RCTResizeImageIfNeeded(UIImage *image, CGSize size, CGFloat scale, RCTResizeMode resizeMode)
348+
static RCTPlatformImage *RCTResizeImageIfNeeded(RCTPlatformImage *image, CGSize size, CGFloat scale, RCTResizeMode resizeMode) // [macOS]
349349
{
350350
if (CGSizeEqualToSize(size, CGSizeZero) || CGSizeEqualToSize(image.size, CGSizeZero) ||
351351
CGSizeEqualToSize(image.size, size)) {
@@ -419,7 +419,7 @@ - (nullable RCTImageLoaderCancellationBlock)loadImageWithURLRequest:(NSURLReques
419419
attribution:{}
420420
progressBlock:progressBlock
421421
partialLoadBlock:partialLoadBlock
422-
completionBlock:^(NSError *error, UIImage *image, id metadata) {
422+
completionBlock:^(NSError *error, RCTPlatformImage *image, id metadata) { // [macOS]
423423
completionBlock(error, image);
424424
}];
425425
return ^{
@@ -555,7 +555,7 @@ - (RCTImageURLLoaderRequest *)_loadImageOrDataWithURLRequest:(NSURLRequest *)req
555555
}
556556

557557
if (cacheResult && partialLoadHandler) {
558-
UIImage *image = [[self imageCache] imageForUrl:request.URL.absoluteString
558+
RCTPlatformImage *image = [[self imageCache] imageForUrl:request.URL.absoluteString // [macOS]
559559
size:size
560560
scale:scale
561561
resizeMode:resizeMode];
@@ -577,7 +577,7 @@ - (RCTImageURLLoaderRequest *)_loadImageOrDataWithURLRequest:(NSURLRequest *)req
577577

578578
// If we've received an image, we should try to set it synchronously,
579579
// if it's data, do decoding on a background thread.
580-
if (RCTIsMainQueue() && ![imageOrData isKindOfClass:[UIImage class]]) {
580+
if (RCTIsMainQueue() && ![imageOrData isKindOfClass:[RCTPlatformImage class]]) { // [macOS]
581581
// Most loaders do not return on the main thread, so caller is probably not
582582
// expecting it, and may do expensive post-processing in the callback
583583
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
@@ -608,7 +608,7 @@ - (RCTImageURLLoaderRequest *)_loadImageOrDataWithURLRequest:(NSURLRequest *)req
608608
attribution:attributionCopy
609609
progressHandler:progressHandler
610610
partialLoadHandler:partialLoadHandler
611-
completionHandler:^(NSError *error, UIImage *image, id metadata) {
611+
completionHandler:^(NSError *error, RCTPlatformImage *image, id metadata) { // [macOS]
612612
completionHandler(error, image, metadata, nil);
613613
}];
614614
}
@@ -618,7 +618,7 @@ - (RCTImageURLLoaderRequest *)_loadImageOrDataWithURLRequest:(NSURLRequest *)req
618618
resizeMode:resizeMode
619619
progressHandler:progressHandler
620620
partialLoadHandler:partialLoadHandler
621-
completionHandler:^(NSError *error, UIImage *image) {
621+
completionHandler:^(NSError *error, RCTPlatformImage *image) { // [macOS]
622622
completionHandler(error, image, nil, nil);
623623
}];
624624
return [[RCTImageURLLoaderRequest alloc] initWithRequestId:nil imageURL:request.URL cancellationBlock:cb];
@@ -649,7 +649,7 @@ - (RCTImageURLLoaderRequest *)_loadImageOrDataWithURLRequest:(NSURLRequest *)req
649649
attribution:attributionCopy
650650
progressHandler:progressHandler
651651
partialLoadHandler:partialLoadHandler
652-
completionHandler:^(NSError *error, UIImage *image, id metadata) {
652+
completionHandler:^(NSError *error, RCTPlatformImage *image, id metadata) { // [macOS]
653653
completionHandler(error, image, metadata, nil);
654654
}];
655655
cancelLoadLocal = loaderRequest.cancellationBlock;
@@ -660,15 +660,15 @@ - (RCTImageURLLoaderRequest *)_loadImageOrDataWithURLRequest:(NSURLRequest *)req
660660
resizeMode:resizeMode
661661
progressHandler:progressHandler
662662
partialLoadHandler:partialLoadHandler
663-
completionHandler:^(NSError *error, UIImage *image) {
663+
completionHandler:^(NSError *error, RCTPlatformImage *image) { // [macOS]
664664
completionHandler(error, image, nil, nil);
665665
}];
666666
}
667667
[cancelLoadLock lock];
668668
cancelLoad = cancelLoadLocal;
669669
[cancelLoadLock unlock];
670670
} else {
671-
UIImage *image;
671+
RCTPlatformImage *image; // [macOS]
672672
if (cacheResult) {
673673
image = [[strongSelf imageCache] imageForUrl:request.URL.absoluteString
674674
size:size
@@ -858,15 +858,15 @@ - (RCTImageURLLoaderRequest *)loadImageWithURLRequest:(NSURLRequest *)imageURLRe
858858
return;
859859
}
860860

861-
if (!imageOrData || [imageOrData isKindOfClass:[UIImage class]]) {
861+
if (!imageOrData || [imageOrData isKindOfClass:[RCTPlatformImage class]]) { // [macOS]
862862
[cancelLoadLock lock];
863863
cancelLoad = nil;
864864
[cancelLoadLock unlock];
865865
completionBlock(error, imageOrData, imageMetadata);
866866
return;
867867
}
868868

869-
RCTImageLoaderCompletionBlock decodeCompletionHandler = ^(NSError *error_, UIImage *image) {
869+
RCTImageLoaderCompletionBlock decodeCompletionHandler = ^(NSError *error_, RCTPlatformImage *image) { // [macOS]
870870
if (cacheResult && image) {
871871
// Store decoded image in cache
872872
[[strongSelf imageCache] addImageToCache:image
@@ -971,7 +971,7 @@ - (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)data
971971
}
972972

973973
auto cancelled = std::make_shared<std::atomic<int>>(0);
974-
void (^completionHandler)(NSError *, UIImage *) = ^(NSError *error, UIImage *image) {
974+
void (^completionHandler)(NSError *, RCTPlatformImage *) = ^(NSError *error, RCTPlatformImage *image) { // [macOS]
975975
if (RCTIsMainQueue()) {
976976
// Most loaders do not return on the main thread, so caller is probably not
977977
// expecting it, and may do expensive post-processing in the callback
@@ -1006,7 +1006,7 @@ - (RCTImageLoaderCancellationBlock)decodeImageData:(NSData *)data
10061006
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
10071007
if (!std::atomic_load(cancelled.get())) {
10081008
// Decompress the image data (this may be CPU and memory intensive)
1009-
UIImage *image = RCTDecodeImageWithData(data, size, scale, resizeMode);
1009+
RCTPlatformImage *image = RCTDecodeImageWithData(data, size, scale, resizeMode); // [macOS]
10101010

10111011
#if !TARGET_OS_OSX && RCT_DEV // [macOS]
10121012
CGSize imagePixelSize = RCTSizeInPixels(image.size, UIImageGetScale(image)); // [macOS]
@@ -1101,7 +1101,7 @@ - (RCTImageLoaderCancellationBlock)getImageSizeForURLRequest:(NSURLRequest *)ima
11011101
break;
11021102
}
11031103
} else {
1104-
UIImage *image = imageOrData;
1104+
RCTPlatformImage *image = imageOrData; // [macOS]
11051105
#if !TARGET_OS_OSX // [macOS]
11061106
CGFloat imageScale = image.scale;
11071107
#else // [macOS
@@ -1195,7 +1195,7 @@ - (id)sendRequest:(NSURLRequest *)request withDelegate:(id<RCTURLRequestDelegate
11951195
{
11961196
__block RCTImageLoaderCancellationBlock requestToken;
11971197
requestToken = [self loadImageWithURLRequest:request
1198-
callback:^(NSError *error, UIImage *image) {
1198+
callback:^(NSError *error, RCTPlatformImage *image) { // [macOS]
11991199
if (error) {
12001200
[delegate URLRequest:requestToken didCompleteWithError:error];
12011201
return;
@@ -1305,7 +1305,7 @@ - (void)cancelRequest:(id)requestToken
13051305
}
13061306
progressBlock:nil
13071307
partialLoadBlock:nil
1308-
completionBlock:^(NSError *error, UIImage *image, id completionMetadata) {
1308+
completionBlock:^(NSError *error, RCTPlatformImage *image, id completionMetadata) { // [macOS]
13091309
if (error) {
13101310
reject(@"E_PREFETCH_FAILURE", nil, error);
13111311
return;

packages/react-native/Libraries/Image/RCTImageStoreManager.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ RCT_EXTERN void RCTEnableImageStoreManagerStorageQueue(BOOL enabled);
2727
* Convenience method to store an image directly (image is converted to data
2828
* internally, so any metadata such as scale or orientation will be lost).
2929
*/
30-
- (void)storeImage:(UIImage *)image withBlock:(void (^)(NSString *imageTag))block;
30+
- (void)storeImage:(RCTPlatformImage *)image withBlock:(void (^)(NSString *imageTag))block; // [macOS]
3131

3232
@end
3333

@@ -36,9 +36,9 @@ RCT_EXTERN void RCTEnableImageStoreManagerStorageQueue(BOOL enabled);
3636
/**
3737
* These methods are deprecated - use the data-based alternatives instead.
3838
*/
39-
- (NSString *)storeImage:(UIImage *)image __deprecated;
40-
- (UIImage *)imageForTag:(NSString *)imageTag __deprecated;
41-
- (void)getImageForTag:(NSString *)imageTag withBlock:(void (^)(UIImage *image))block __deprecated;
39+
- (NSString *)storeImage:(RCTPlatformImage *)image __deprecated; // [macOS]
40+
- (RCTPlatformImage *)imageForTag:(NSString *)imageTag __deprecated; // [macOS]
41+
- (void)getImageForTag:(NSString *)imageTag withBlock:(void (^)(RCTPlatformImage *image))block __deprecated; // [macOS]
4242

4343
@end
4444

0 commit comments

Comments
 (0)