Skip to content

Commit 643c8b6

Browse files
committed
iOS new arch (with old as fallback)
1 parent 4291eff commit 643c8b6

File tree

9 files changed

+121
-62
lines changed

9 files changed

+121
-62
lines changed
File renamed without changes.

ios/RNCImageEditor.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@
55
* LICENSE file in the root directory of this source tree.
66
*/
77

8+
#ifdef RCT_NEW_ARCH_ENABLED
9+
#import "RNCImageEditorSpec.h"
10+
11+
@interface RNCImageEditor : NSObject <NativeRNCImageEditorSpec>
12+
#else
813
#import <React/RCTBridgeModule.h>
914

1015
@interface RNCImageEditor : NSObject <RCTBridgeModule>
16+
#endif
1117

1218
@end

ios/RNCImageEditor.m renamed to ios/RNCImageEditor.mm

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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
4360
RCT_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

ios/RNCImageEditor.xcodeproj/project.pbxproj

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10-
1478DE2B22A0403F00D818FA /* RNCFileSystem.m in Sources */ = {isa = PBXBuildFile; fileRef = 1478DE2A22A0403F00D818FA /* RNCFileSystem.m */; };
11-
1478DE2E22A044E500D818FA /* RNCImageUtils.m in Sources */ = {isa = PBXBuildFile; fileRef = 1478DE2D22A044E500D818FA /* RNCImageUtils.m */; };
12-
B3E7B58A1CC2AC0600A0062D /* RNCImageEditor.m in Sources */ = {isa = PBXBuildFile; fileRef = B3E7B5891CC2AC0600A0062D /* RNCImageEditor.m */; };
10+
C8EA266C2AF7E7CA008069C8 /* RNCFileSystem.mm in Sources */ = {isa = PBXBuildFile; fileRef = C8EA266B2AF7E7CA008069C8 /* RNCFileSystem.mm */; };
11+
C8EA266E2AF7E7D5008069C8 /* RNCImageEditor.mm in Sources */ = {isa = PBXBuildFile; fileRef = C8EA266D2AF7E7D5008069C8 /* RNCImageEditor.mm */; };
12+
C8EA26702AF7E7DB008069C8 /* RNCImageUtils.mm in Sources */ = {isa = PBXBuildFile; fileRef = C8EA266F2AF7E7DB008069C8 /* RNCImageUtils.mm */; };
1313
/* End PBXBuildFile section */
1414

1515
/* Begin PBXCopyFilesBuildPhase section */
@@ -26,12 +26,12 @@
2626

2727
/* Begin PBXFileReference section */
2828
134814201AA4EA6300B7C361 /* libRNCImageEditor.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNCImageEditor.a; sourceTree = BUILT_PRODUCTS_DIR; };
29-
1478DE2A22A0403F00D818FA /* RNCFileSystem.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNCFileSystem.m; sourceTree = "<group>"; };
3029
1478DE2C22A0406800D818FA /* RNCFileSystem.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNCFileSystem.h; sourceTree = "<group>"; };
31-
1478DE2D22A044E500D818FA /* RNCImageUtils.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNCImageUtils.m; sourceTree = "<group>"; };
3230
1478DE2F22A0450800D818FA /* RNCImageUtils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNCImageUtils.h; sourceTree = "<group>"; };
3331
B3E7B5881CC2AC0600A0062D /* RNCImageEditor.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = RNCImageEditor.h; sourceTree = "<group>"; };
34-
B3E7B5891CC2AC0600A0062D /* RNCImageEditor.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RNCImageEditor.m; sourceTree = "<group>"; };
32+
C8EA266B2AF7E7CA008069C8 /* RNCFileSystem.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RNCFileSystem.mm; sourceTree = "<group>"; };
33+
C8EA266D2AF7E7D5008069C8 /* RNCImageEditor.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RNCImageEditor.mm; sourceTree = "<group>"; };
34+
C8EA266F2AF7E7DB008069C8 /* RNCImageUtils.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = RNCImageUtils.mm; sourceTree = "<group>"; };
3535
/* End PBXFileReference section */
3636

3737
/* Begin PBXFrameworksBuildPhase section */
@@ -56,12 +56,12 @@
5656
58B511D21A9E6C8500147676 = {
5757
isa = PBXGroup;
5858
children = (
59-
1478DE2A22A0403F00D818FA /* RNCFileSystem.m */,
60-
1478DE2D22A044E500D818FA /* RNCImageUtils.m */,
6159
1478DE2F22A0450800D818FA /* RNCImageUtils.h */,
60+
C8EA266F2AF7E7DB008069C8 /* RNCImageUtils.mm */,
6261
1478DE2C22A0406800D818FA /* RNCFileSystem.h */,
62+
C8EA266B2AF7E7CA008069C8 /* RNCFileSystem.mm */,
6363
B3E7B5881CC2AC0600A0062D /* RNCImageEditor.h */,
64-
B3E7B5891CC2AC0600A0062D /* RNCImageEditor.m */,
64+
C8EA266D2AF7E7D5008069C8 /* RNCImageEditor.mm */,
6565
134814211AA4EA7D00B7C361 /* Products */,
6666
);
6767
sourceTree = "<group>";
@@ -105,6 +105,7 @@
105105
developmentRegion = English;
106106
hasScannedForEncodings = 0;
107107
knownRegions = (
108+
English,
108109
en,
109110
);
110111
mainGroup = 58B511D21A9E6C8500147676;
@@ -122,9 +123,9 @@
122123
isa = PBXSourcesBuildPhase;
123124
buildActionMask = 2147483647;
124125
files = (
125-
1478DE2E22A044E500D818FA /* RNCImageUtils.m in Sources */,
126-
1478DE2B22A0403F00D818FA /* RNCFileSystem.m in Sources */,
127-
B3E7B58A1CC2AC0600A0062D /* RNCImageEditor.m in Sources */,
126+
C8EA266C2AF7E7CA008069C8 /* RNCFileSystem.mm in Sources */,
127+
C8EA266E2AF7E7D5008069C8 /* RNCImageEditor.mm in Sources */,
128+
C8EA26702AF7E7DB008069C8 /* RNCImageUtils.mm in Sources */,
128129
);
129130
runOnlyForDeploymentPostprocessing = 0;
130131
};
File renamed without changes.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
"registry": "https://registry.npmjs.org/"
8585
},
8686
"codegenConfig": {
87-
"name": "RNImageEditorSpec",
87+
"name": "RNCImageEditorSpec",
8888
"type": "modules",
8989
"jsSrcsDir": "src"
9090
},

react-native-image-editor.podspec

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require 'json'
22

33
package = JSON.parse(File.read(File.join(__dir__, 'package.json')))
4+
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
45

56
Pod::Spec.new do |s|
67
s.name = "react-native-image-editor"
@@ -13,8 +14,30 @@ Pod::Spec.new do |s|
1314
s.platform = :ios, "9.0"
1415

1516
s.source = { :git => "https://github.com/callstack/react-native-image-editor.git", :tag => "#{s.version}" }
16-
s.source_files = "ios/**/*.{h,m}"
17+
s.source_files = "ios/**/*.{h,m,mm}"
1718

1819
s.dependency 'React'
20+
s.dependency "React-Core"
1921
s.dependency 'React-RCTImage'
22+
23+
# Use install_modules_dependencies helper to install the dependencies if React Native version >=0.71.0.
24+
# See https://github.com/facebook/react-native/blob/febf6b7f33fdb4904669f99d795eba4c0f95d7bf/scripts/cocoapods/new_architecture.rb#L79.
25+
if respond_to?(:install_modules_dependencies, true)
26+
install_modules_dependencies(s)
27+
end
28+
29+
# Don't install the dependencies when we run `pod install` in the old architecture.
30+
if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then
31+
s.compiler_flags = folly_compiler_flags + " -DRCT_NEW_ARCH_ENABLED=1"
32+
s.pod_target_xcconfig = {
33+
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\"",
34+
"OTHER_CPLUSPLUSFLAGS" => "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1",
35+
"CLANG_CXX_LANGUAGE_STANDARD" => "c++17"
36+
}
37+
s.dependency "React-Codegen"
38+
s.dependency "RCT-Folly"
39+
s.dependency "RCTRequired"
40+
s.dependency "RCTTypeSafety"
41+
s.dependency "ReactCommon/turbomodule/core"
42+
end
2043
end

src/NativeRNCImageEditor.ts

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,41 @@ import type { TurboModule } from 'react-native';
22
import type { Double } from 'react-native/Libraries/Types/CodegenTypes';
33
import { TurboModuleRegistry } from 'react-native';
44

5-
export interface ImageCropData {
6-
/**
7-
* The top-left corner of the cropped image, specified in the original
8-
* image's coordinate space.
9-
*/
10-
offset: {
11-
x: Double;
12-
y: Double;
13-
};
14-
/**
15-
* The size (dimensions) of the cropped image, specified in the original
16-
* image's coordinate space.
17-
*/
18-
size: {
19-
width: Double;
20-
height: Double;
21-
};
22-
/**
23-
* (Optional) size to scale the cropped image to.
24-
*/
25-
displaySize?: {
26-
width: Double;
27-
height: Double;
28-
};
29-
/**
30-
* (Optional) the resizing mode to use when scaling the image. If the
31-
* `displaySize` param is not specified, this has no effect.
32-
*/
33-
resizeMode?: string;
34-
}
35-
365
export interface Spec extends TurboModule {
37-
cropImage(uri: string, cropData: ImageCropData): Promise<string>;
6+
cropImage(
7+
uri: string,
8+
cropData: {
9+
// inlined `ImageCropData` type (for older RN versions 71 and below)
10+
/**
11+
* The top-left corner of the cropped image, specified in the original
12+
* image's coordinate space.
13+
*/
14+
offset: {
15+
x: Double;
16+
y: Double;
17+
};
18+
/**
19+
* The size (dimensions) of the cropped image, specified in the original
20+
* image's coordinate space.
21+
*/
22+
size: {
23+
width: Double;
24+
height: Double;
25+
};
26+
/**
27+
* (Optional) size to scale the cropped image to.
28+
*/
29+
displaySize?: {
30+
width: Double;
31+
height: Double;
32+
};
33+
/**
34+
* (Optional) the resizing mode to use when scaling the image. If the
35+
* `displaySize` param is not specified, this has no effect.
36+
*/
37+
resizeMode?: string;
38+
}
39+
): Promise<string>;
3840
}
3941

4042
export default TurboModuleRegistry.getEnforcing<Spec>('RNCImageEditor');

src/index.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66
*
77
*/
88
import { NativeModules, Platform } from 'react-native';
9-
import type {
10-
ImageCropData as ImageCropDataSpec,
11-
Spec,
12-
} from './NativeRNCImageEditor';
9+
import type { Spec } from './NativeRNCImageEditor';
1310

1411
const LINKING_ERROR =
1512
`The package '@react-native-community/image-editor' doesn't seem to be linked. Make sure: \n\n` +
@@ -35,8 +32,10 @@ const RNCImageEditor: Spec = RNCImageEditorModule
3532
}
3633
);
3734

35+
type ImageCropDataFromSpec = Parameters<Spec['cropImage']>[1];
36+
3837
export interface ImageCropData
39-
extends Pick<ImageCropDataSpec, 'offset' | 'size' | 'displaySize'> {
38+
extends Pick<ImageCropDataFromSpec, 'offset' | 'size' | 'displaySize'> {
4039
resizeMode?: 'contain' | 'cover' | 'stretch';
4140
// ^^^ codegen doesn't support union types yet
4241
// so to provide more type safety we override the type here

0 commit comments

Comments
 (0)