Skip to content

Commit 254f67f

Browse files
author
JimmyDaddy
committed
feat: IOS code opt
1 parent 27a581f commit 254f67f

16 files changed

+713
-438
lines changed

ios/Podfile

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
require_relative '../node_modules/react-native/scripts/react_native_pods'
2+
require_relative '../node_modules/@react-native-community/cli-platform-ios/native_modules'
3+
4+
platform :ios, min_ios_version_supported
5+
prepare_react_native_project!
6+
7+
# If you are using a `react-native-flipper` your iOS build will fail when `NO_FLIPPER=1` is set.
8+
# because `react-native-flipper` depends on (FlipperKit,...) that will be excluded
9+
#
10+
# To fix this you can also exclude `react-native-flipper` using a `react-native.config.js`
11+
# ```js
12+
# module.exports = {
13+
# dependencies: {
14+
# ...(process.env.NO_FLIPPER ? { 'react-native-flipper': { platforms: { ios: null } } } : {}),
15+
# ```
16+
flipper_config = ENV['NO_FLIPPER'] == "1" ? FlipperConfiguration.disabled : FlipperConfiguration.enabled
17+
18+
linkage = ENV['USE_FRAMEWORKS']
19+
if linkage != nil
20+
Pod::UI.puts "Configuring Pod with #{linkage}ally linked Frameworks".green
21+
use_frameworks! :linkage => linkage.to_sym
22+
end
23+
24+
target 'ImageMarkerExample' do
25+
config = use_native_modules!
26+
27+
# Flags change depending on the env values.
28+
flags = get_default_flags()
29+
30+
use_react_native!(
31+
:path => config[:reactNativePath],
32+
# Hermes is now enabled by default. Disable by setting this flag to false.
33+
# Upcoming versions of React Native may rely on get_default_flags(), but
34+
# we make it explicit here to aid in the React Native upgrade process.
35+
:hermes_enabled => flags[:hermes_enabled],
36+
:fabric_enabled => flags[:fabric_enabled],
37+
# Enables Flipper.
38+
#
39+
# Note that if you have use_frameworks! enabled, Flipper will not work and
40+
# you should disable the next line.
41+
:flipper_configuration => flipper_config,
42+
# An absolute path to your application root.
43+
:app_path => "#{Pod::Config.instance.installation_root}/.."
44+
)
45+
46+
target 'ImageMarkerExampleTests' do
47+
inherit! :complete
48+
# Pods for testing
49+
end
50+
51+
post_install do |installer|
52+
react_native_post_install(
53+
installer,
54+
# Set `mac_catalyst_enabled` to `true` in order to apply patches
55+
# necessary for Mac Catalyst builds
56+
:mac_catalyst_enabled => false
57+
)
58+
__apply_Xcode_12_5_M1_post_install_workaround(installer)
59+
end
60+
end

ios/RCTImageMarker.xcodeproj/project.pbxproj

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
38E5F5F01D3DCDBF00AA7E71 /* RCTImageMarker.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 38E5F5EF1D3DCDBF00AA7E71 /* RCTImageMarker.h */; };
1111
38E5F5F21D3DCDBF00AA7E71 /* RCTImageMarker.m in Sources */ = {isa = PBXBuildFile; fileRef = 38E5F5F11D3DCDBF00AA7E71 /* RCTImageMarker.m */; };
1212
38E5F6301D3DDD7100AA7E71 /* RCTConvert+ImageMarker.m in Sources */ = {isa = PBXBuildFile; fileRef = 38E5F62F1D3DDD7100AA7E71 /* RCTConvert+ImageMarker.m */; };
13+
A1261EA42A447F4D0043788B /* MarkImageOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = A1261EA02A447F4D0043788B /* MarkImageOptions.m */; };
14+
A1261EA52A447F4D0043788B /* MarkTextOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = A1261EA22A447F4D0043788B /* MarkTextOptions.m */; };
15+
A198EF4C2A44270E006D3B9A /* TextBackground.m in Sources */ = {isa = PBXBuildFile; fileRef = A198EF4B2A44270E006D3B9A /* TextBackground.m */; };
16+
A198EF552A443425006D3B9A /* Utils.m in Sources */ = {isa = PBXBuildFile; fileRef = A198EF542A443425006D3B9A /* Utils.m */; };
17+
A198EF572A44347E006D3B9A /* Options.m in Sources */ = {isa = PBXBuildFile; fileRef = A198EF562A44347E006D3B9A /* Options.m */; };
1318
/* End PBXBuildFile section */
1419

1520
/* Begin PBXCopyFilesBuildPhase section */
@@ -31,6 +36,16 @@
3136
38E5F5F11D3DCDBF00AA7E71 /* RCTImageMarker.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RCTImageMarker.m; sourceTree = "<group>"; };
3237
38E5F62E1D3DDD2300AA7E71 /* RCTConvert+ImageMarker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "RCTConvert+ImageMarker.h"; sourceTree = "<group>"; };
3338
38E5F62F1D3DDD7100AA7E71 /* RCTConvert+ImageMarker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "RCTConvert+ImageMarker.m"; sourceTree = "<group>"; };
39+
A1261EA02A447F4D0043788B /* MarkImageOptions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MarkImageOptions.m; sourceTree = "<group>"; };
40+
A1261EA12A447F4D0043788B /* MarkTextOptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MarkTextOptions.h; sourceTree = "<group>"; };
41+
A1261EA22A447F4D0043788B /* MarkTextOptions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MarkTextOptions.m; sourceTree = "<group>"; };
42+
A1261EA32A447F4D0043788B /* MarkImageOptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MarkImageOptions.h; sourceTree = "<group>"; };
43+
A198EF492A441F5E006D3B9A /* Options.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Options.h; sourceTree = "<group>"; };
44+
A198EF4A2A44208C006D3B9A /* TextBackground.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TextBackground.h; sourceTree = "<group>"; };
45+
A198EF4B2A44270E006D3B9A /* TextBackground.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TextBackground.m; sourceTree = "<group>"; };
46+
A198EF532A443425006D3B9A /* Utils.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = Utils.h; sourceTree = "<group>"; };
47+
A198EF542A443425006D3B9A /* Utils.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Utils.m; sourceTree = "<group>"; };
48+
A198EF562A44347E006D3B9A /* Options.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = Options.m; sourceTree = "<group>"; };
3449
/* End PBXFileReference section */
3550

3651
/* Begin PBXFrameworksBuildPhase section */
@@ -67,6 +82,16 @@
6782
38E5F5F11D3DCDBF00AA7E71 /* RCTImageMarker.m */,
6883
38E5F62E1D3DDD2300AA7E71 /* RCTConvert+ImageMarker.h */,
6984
38E5F62F1D3DDD7100AA7E71 /* RCTConvert+ImageMarker.m */,
85+
A198EF4A2A44208C006D3B9A /* TextBackground.h */,
86+
A198EF4B2A44270E006D3B9A /* TextBackground.m */,
87+
A198EF532A443425006D3B9A /* Utils.h */,
88+
A198EF542A443425006D3B9A /* Utils.m */,
89+
A1261EA32A447F4D0043788B /* MarkImageOptions.h */,
90+
A1261EA02A447F4D0043788B /* MarkImageOptions.m */,
91+
A1261EA12A447F4D0043788B /* MarkTextOptions.h */,
92+
A1261EA22A447F4D0043788B /* MarkTextOptions.m */,
93+
A198EF492A441F5E006D3B9A /* Options.h */,
94+
A198EF562A44347E006D3B9A /* Options.m */,
7095
);
7196
path = RCTImageMarker;
7297
sourceTree = "<group>";
@@ -129,7 +154,12 @@
129154
buildActionMask = 2147483647;
130155
files = (
131156
38E5F5F21D3DCDBF00AA7E71 /* RCTImageMarker.m in Sources */,
157+
A198EF4C2A44270E006D3B9A /* TextBackground.m in Sources */,
158+
A198EF572A44347E006D3B9A /* Options.m in Sources */,
132159
38E5F6301D3DDD7100AA7E71 /* RCTConvert+ImageMarker.m in Sources */,
160+
A198EF552A443425006D3B9A /* Utils.m in Sources */,
161+
A1261EA42A447F4D0043788B /* MarkImageOptions.m in Sources */,
162+
A1261EA52A447F4D0043788B /* MarkTextOptions.m in Sources */,
133163
);
134164
runOnlyForDeploymentPostprocessing = 0;
135165
};

ios/RCTImageMarker/MarkImageOptions.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//
2+
// Options+ImageMarkOptions.h
3+
// react-native-image-marker
4+
//
5+
// Created by Jimmydaddy on 2023/6/22.
6+
//
7+
8+
#import "Options.h"
9+
10+
NS_ASSUME_NONNULL_BEGIN
11+
12+
@interface MarkImageOptions: Options
13+
14+
@property (nonatomic, copy) NSDictionary * markerSrc;
15+
@property (nonatomic, copy) NSString * markerImageUri;
16+
@property (nonatomic, assign) CGFloat markerScale;
17+
18+
@end
19+
20+
NS_ASSUME_NONNULL_END

ios/RCTImageMarker/MarkImageOptions.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// Options+ImageMarkOptions.m
3+
// react-native-image-marker
4+
//
5+
// Created by Jimmydaddy on 2023/6/22.
6+
//
7+
8+
#import "MarkImageOptions.h"
9+
#import <RCTConvert+ImageMarker.h>
10+
11+
@implementation MarkImageOptions: Options
12+
13+
-(id)initWithDicOpts:(NSDictionary *)opts
14+
{
15+
self = [super initWithDicOpts: opts];
16+
if (![[opts allKeys] containsObject:@"markerSrc"] || opts[@"markerSrc"] == nil) {
17+
@throw [NSException exceptionWithName:@"PARAMS_REQUIRED" reason:@"marker image is required" userInfo:nil];
18+
}
19+
_markerSrc = opts[@"markerSrc"];
20+
_markerImageUri = _markerSrc[@"uri"];
21+
_markerScale = [opts objectForKey:@"markerScale"]? [RCTConvert CGFloat: opts[@"markerScale"]] : 1;
22+
return self;
23+
}
24+
25+
+(id)checkParams: (NSDictionary *) opts rejecter:(RCTPromiseRejectBlock)reject
26+
{
27+
@try {
28+
return [[MarkImageOptions alloc] initWithDicOpts: opts];
29+
}
30+
@catch (NSException *exception) {
31+
reject(exception.name, exception.reason, nil);
32+
}
33+
}
34+
35+
36+
@end

ios/RCTImageMarker/MarkTextOptions.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// Options+TextMarkOptions.h
3+
// RCTImageMarker
4+
//
5+
// Created by Jimmydaddy on 2023/6/22.
6+
// Copyright © 2023 Jimmy. All rights reserved.
7+
//
8+
9+
#import "Options.h"
10+
11+
NS_ASSUME_NONNULL_BEGIN
12+
13+
@interface MarkTextOptions: Options
14+
@property (nonatomic, copy) NSString * text;
15+
@end
16+
17+
NS_ASSUME_NONNULL_END

ios/RCTImageMarker/MarkTextOptions.m

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//
2+
// Options+TextMarkOptions.m
3+
// RCTImageMarker
4+
//
5+
// Created by Jimmydaddy on 2023/6/22.
6+
// Copyright © 2023 Jimmy. All rights reserved.
7+
//
8+
9+
#import "MarkTextOptions.h"
10+
11+
@implementation MarkTextOptions: Options
12+
13+
-(id)initWithDicOpts:(NSDictionary *)opts
14+
{
15+
self = [super initWithDicOpts: opts];
16+
if (![[opts allKeys] containsObject:@"text"] || opts[@"text"] == nil) {
17+
@throw [NSException exceptionWithName:@"PARAMS_REQUIRED" reason:@"text is required" userInfo:nil];
18+
}
19+
_text = opts[@"text"];
20+
return self;
21+
}
22+
23+
+(id)checkParams: (NSDictionary *) opts rejecter:(RCTPromiseRejectBlock)reject
24+
{
25+
@try {
26+
return [[MarkTextOptions alloc] initWithDicOpts: opts];
27+
}
28+
@catch (NSException *exception) {
29+
reject(exception.name, exception.reason, nil);
30+
}
31+
}
32+
33+
@end

ios/RCTImageMarker/Options.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// NSObject+Options.h
3+
// RCTImageMarker
4+
//
5+
// Created by Jimmydaddy on 2023/6/22.
6+
// Copyright © 2023 Jimmy. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import <React/RCTBridgeModule.h>
11+
#import "TextBackground.h"
12+
#import "RCTConvert+ImageMarker.h"
13+
14+
NS_ASSUME_NONNULL_BEGIN
15+
16+
@interface Options: NSObject
17+
18+
@property (nonatomic, copy) NSDictionary * src;
19+
@property (nonatomic, copy) UIColor * color;
20+
@property (nonatomic, copy) NSString * uri;
21+
@property (nonatomic, copy) UIFont * font;
22+
@property (nonatomic, copy) NSShadow * shadow;
23+
@property (nonatomic, copy) TextBackground* textBackground;
24+
@property (nonatomic, assign) CGFloat scale;
25+
@property (nonatomic, assign) NSInteger quality;
26+
@property (nonatomic, copy) NSString * filename;
27+
@property (nonatomic, assign) NSInteger maxSize;
28+
@property (nonatomic, assign) CGFloat X;
29+
@property (nonatomic, assign) CGFloat Y;
30+
@property (nonatomic, copy) NSString * saveFormat;
31+
@property (nonatomic, assign) MarkerPosition position;
32+
33+
-(id)initWithDicOpts: (NSDictionary *) opts;
34+
35+
+(id)checkParams: (NSDictionary *) opts rejecter:(RCTPromiseRejectBlock)reject;
36+
37+
@end
38+
39+
NS_ASSUME_NONNULL_END

ios/RCTImageMarker/Options.m

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//
2+
// NSObject+Options.m
3+
// RCTImageMarker
4+
//
5+
// Created by Jimmydaddy on 2023/6/22.
6+
// Copyright © 2023 Jimmy. All rights reserved.
7+
//
8+
9+
#import "Options.h"
10+
#import "Utils.h"
11+
#import <UIKit/UIKit.h>
12+
#import "RCTConvert+ImageMarker.h"
13+
14+
@implementation Options
15+
16+
-(id) initWithDicOpts:(NSDictionary *)opts
17+
{
18+
if (![[opts allKeys] containsObject:@"src"] || opts[@"src"] == nil) {
19+
@throw [NSException exceptionWithName:@"PARAMS_REQUIRED" reason:@"image is required" userInfo:nil];
20+
}
21+
_src = opts[@"src"];
22+
_uri = _src[@"uri"];
23+
_color = [Utils getColor: opts[@"color"]];
24+
_shadow = [Utils getShadowStyle: opts[@"shadowStyle"]];
25+
_textBackground = [[TextBackground alloc] initWithTextBackgroundStyle: opts[@"textBackgroundStyle"]];
26+
_X = [RCTConvert CGFloat:opts[@"X"]];
27+
_Y = [RCTConvert CGFloat:opts[@"Y"]];
28+
_font = [UIFont fontWithName:opts[@"fontName"] size: [opts objectForKey: @"fontSize"]? [RCTConvert CGFloat: [opts objectForKey: @"fontSize"]]: 14.0];
29+
_scale = [opts objectForKey: @"scale"]? [RCTConvert CGFloat: [opts objectForKey: @"scale"]]: 1.0;
30+
_quality = [opts objectForKey: @"quality"]? [RCTConvert NSInteger: [opts objectForKey: @"quality"]]: 100;
31+
_saveFormat = opts[@"saveFormat"];
32+
_maxSize = [RCTConvert NSInteger: opts[@"maxSize"]];
33+
_filename = opts[@"fileName"];
34+
_position = [RCTConvert MarkerPosition: opts[@"position"]];
35+
36+
return self;
37+
}
38+
39+
+(id)checkParams: (NSDictionary *) opts rejecter:(RCTPromiseRejectBlock)reject
40+
{
41+
@try {
42+
return [[Options alloc] initWithDicOpts: opts];
43+
}
44+
@catch (NSException *exception) {
45+
reject(exception.name, exception.reason, nil);
46+
}
47+
}
48+
49+
@end

ios/RCTImageMarker/RCTConvert+ImageMarker.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,22 @@
1010
#define RCTConvert_ImageMarker_h
1111
#import <React/RCTConvert.h>
1212

13+
typedef enum{
14+
TopLeft = 0,
15+
TopCenter = 1,
16+
TopRight = 2,
17+
BottomLeft = 3,
18+
BottomCenter = 4,
19+
BottomRight = 5,
20+
Center = 6
21+
} MarkerPosition;
22+
1323
@interface RCTConvert(ImageMarker)
1424
+ (CGPoint)CGPoint:(id)json offset:(NSUInteger)offset;
1525
+ (CGRect)CGRect:(id)json offset:(NSUInteger)offset;
1626
+ (CGColorRef)CGColor:(id)json offset:(NSUInteger)offset;
1727
+ (CGSize)CGSize:(id)json offset:(NSUInteger)offset;
28+
+ (MarkerPosition)MarkerPosition: (id)value;
1829
@end
1930

2031
#endif /* RCTConvert_ImageMarker_h */

ios/RCTImageMarker/RCTConvert+ImageMarker.m

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ + (CGPoint)CGPoint:(id)json offset:(NSUInteger)offset
3636
};
3737
}
3838

39-
4039
+ (CGRect)CGRect:(id)json offset:(NSUInteger)offset
4140
{
4241
NSArray *arr = [self NSArray:json];
@@ -59,4 +58,20 @@ + (CGColorRef)CGColor:(id)json offset:(NSUInteger)offset
5958
}
6059
return [self CGColor:[arr subarrayWithRange:(NSRange){offset, 4}]];
6160
}
61+
62+
+ (MarkerPosition)MarkerPosition: (id _Nullable)value
63+
{
64+
NSDictionary *MyEnumMap = @{
65+
@"topLeft": @(TopLeft),
66+
@"topRight": @(TopRight),
67+
@"topCenter": @(TopCenter),
68+
@"center": @(Center),
69+
@"bottomCenter": @(BottomCenter),
70+
@"bottomLeft": @(BottomLeft),
71+
@"bottomRight": @(BottomRight),
72+
};
73+
MarkerPosition mv = [MyEnumMap[value] integerValue];
74+
75+
return mv != nil? mv : TopLeft ;
76+
}
6277
@end

ios/RCTImageMarker/RCTImageMarker.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,6 @@
1010
#import <React/RCTBridge.h>
1111
#import <React/RCTImageLoader.h>
1212

13-
14-
1513
@interface ImageMarker : NSObject <RCTBridgeModule>
1614

1715
@end
18-
19-
@interface TextBackground : NSObject
20-
@property (copy) UIColor * colorBg;
21-
@property (copy) NSString * typeBg;
22-
@property (assign) float paddingX;
23-
@property (assign) float paddingY;
24-
@end

0 commit comments

Comments
 (0)