@@ -40,15 +40,37 @@ @implementation RNCImageEditor
4040 * be scaled down to `displaySize` rather than `size`.
4141 * All units are in px (not points).
4242 */
43+ #ifdef RCT_NEW_ARCH_ENABLED
44+ - (void ) cropImage : (NSString *)uri
45+ cropData : (JS::NativeRNCImageEditor::SpecCropImageCropData &)data
46+ resolve : (RCTPromiseResolveBlock)resolve
47+ reject : (RCTPromiseRejectBlock)reject
48+ {
49+ CGSize size = [RCTConvert CGSize: @{ @" width" : @(data.size ().width ()), @" height" : @(data.size ().height ()) }];
50+ CGPoint offset = [RCTConvert CGPoint: @{ @" x" : @(data.offset ().x ()), @" y" : @(data.offset ().y ()) }];
51+ CGSize targetSize = size;
52+ if (data.displaySize ().has_value ()) {
53+ JS::NativeRNCImageEditor::SpecCropImageCropDataDisplaySize displaySize = *data.displaySize (); // Extract the value from the optional
54+ // in pixels
55+ targetSize = [RCTConvert CGSize: @{ @" width" : @(displaySize.width ()), @" height" : @(displaySize.height ()) }];
56+ }
57+ NSString *displaySize = data.resizeMode ();
58+ NSURLRequest *imageRequest = [NSURLRequest requestWithURL: [NSURL URLWithString: uri]];
59+ #else
4360RCT_EXPORT_METHOD (cropImage:(NSURLRequest *)imageRequest
4461 cropData:(NSDictionary *)cropData
4562 resolve:(RCTPromiseResolveBlock)resolve
4663 reject:(RCTPromiseRejectBlock)reject)
4764{
48- CGRect rect = {
49- [RCTConvert CGPoint: cropData[@" offset" ]],
50- [RCTConvert CGSize: cropData[@" size" ]]
51- };
65+ CGSize size = [RCTConvert CGSize: cropData[@" size" ]];
66+ CGPoint offset = [RCTConvert CGPoint: cropData[@" offset" ]];
67+ CGSize targetSize = size;
68+ NSString *displaySize = cropData[@" resizeMode" ];
69+ if (displaySize){
70+ targetSize = [RCTConvert CGSize: cropData[@" displaySize" ]];
71+ }
72+ #endif
73+ CGRect rect = {offset,size};
5274 NSURL *url = [imageRequest URL ];
5375 NSString *urlPath = [url path ];
5476 NSString *extension = [urlPath pathExtension ];
@@ -60,15 +82,13 @@ @implementation RNCImageEditor
6082 }
6183
6284 // Crop image
63- CGSize targetSize = rect.size ;
6485 CGRect targetRect = {{-rect.origin .x , -rect.origin .y }, image.size };
6586 CGAffineTransform transform = RCTTransformFromTargetRect (image.size , targetRect);
6687 UIImage *croppedImage = RCTTransformImage (image, targetSize, image.scale , transform);
6788
6889 // Scale image
69- if (cropData[@" displaySize" ]) {
70- targetSize = [RCTConvert CGSize: cropData[@" displaySize" ]]; // in pixels
71- RCTResizeMode resizeMode = [RCTConvert RCTResizeMode: cropData[@" resizeMode" ] ?: @" contain" ];
90+ if (displaySize) {
91+ RCTResizeMode resizeMode = [RCTConvert RCTResizeMode: displaySize ?: @" contain" ];
7292 targetRect = RCTTargetRect (croppedImage.size , targetSize, 1 , resizeMode);
7393 transform = RCTTransformFromTargetRect (croppedImage.size , targetRect);
7494 croppedImage = RCTTransformImage (croppedImage, targetSize, image.scale , transform);
@@ -77,7 +97,7 @@ @implementation RNCImageEditor
7797 // Store image
7898 NSString *path = NULL ;
7999 NSData *imageData = NULL ;
80-
100+
81101 if ([extension isEqualToString: @" png" ]){
82102 imageData = UIImagePNGRepresentation (croppedImage);
83103 path = [RNCFileSystem generatePathInDirectory: [[RNCFileSystem cacheDirectoryPath ] stringByAppendingPathComponent: @" ReactNative_cropped_image_" ] withExtension: @" .png" ];
@@ -90,14 +110,22 @@ @implementation RNCImageEditor
90110
91111 NSError *writeError;
92112 NSString *uri = [RNCImageUtils writeImage: imageData toPath: path error: &writeError];
93-
113+
94114 if (writeError != nil ) {
95115 reject (@(writeError.code ).stringValue , writeError.description , writeError);
96116 return ;
97117 }
98-
118+
99119 resolve (uri);
100120 }];
101121}
102122
123+ #ifdef RCT_NEW_ARCH_ENABLED
124+ - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
125+ (const facebook::react::ObjCTurboModule::InitParams &)params
126+ {
127+ return std::make_shared<facebook::react::NativeRNCImageEditorSpecJSI>(params);
128+ }
129+ #endif
130+
103131@end
0 commit comments