@@ -23,11 +23,11 @@ bool saveImage(NSString * fullPath, UIImage * image, NSString * format, float qu
2323 } else if ([format isEqualToString: @" PNG" ]) {
2424 data = UIImagePNGRepresentation (image);
2525 }
26-
26+
2727 if (data == nil ) {
2828 return NO ;
2929 }
30-
30+
3131 NSFileManager * fileManager = [NSFileManager defaultManager ];
3232 return [fileManager createFileAtPath: fullPath contents: data attributes: nil ];
3333}
@@ -47,7 +47,7 @@ bool saveImage(NSString * fullPath, UIImage * image, NSString * format, float qu
4747 } else {
4848 directory = [documentsDirectory stringByAppendingPathComponent: outputPath];
4949 }
50-
50+
5151 NSError *error;
5252 [[NSFileManager defaultManager ] createDirectoryAtPath: directory withIntermediateDirectories: YES attributes: nil error: &error];
5353 if (error) {
@@ -70,14 +70,14 @@ bool saveImage(NSString * fullPath, UIImage * image, NSString * format, float qu
7070 const int rotDiv90 = (int )round (rotationDegrees / 90 );
7171 const int rotQuadrant = rotDiv90 % 4 ;
7272 const int rotQuadrantAbs = (rotQuadrant < 0 ) ? rotQuadrant + 4 : rotQuadrant;
73-
73+
7474 // Return the input image if no rotation specified.
7575 if (0 == rotQuadrantAbs) {
7676 return inputImage;
7777 } else {
7878 // Rotate the image by 80, 180, 270.
7979 UIImageOrientation orientation = UIImageOrientationUp;
80-
80+
8181 switch (rotQuadrantAbs) {
8282 case 1 :
8383 orientation = UIImageOrientationRight; // 90 deg CW
@@ -89,13 +89,63 @@ bool saveImage(NSString * fullPath, UIImage * image, NSString * format, float qu
8989 orientation = UIImageOrientationLeft; // 90 deg CCW
9090 break ;
9191 }
92-
92+
9393 return [[UIImage alloc ] initWithCGImage: inputImage.CGImage
9494 scale: 1.0
9595 orientation: orientation];
9696 }
9797}
9898
99+ void transformImage (UIImage *image,
100+ RCTResponseSenderBlock callback,
101+ int rotation,
102+ CGSize newSize,
103+ NSString * fullPath,
104+ NSString * format,
105+ int quality)
106+ {
107+ if (image == nil ) {
108+ callback (@[@" Can't retrieve the file from the path." , @" " ]);
109+ return ;
110+ }
111+
112+ // Rotate image if rotation is specified.
113+ if (0 != (int )rotation) {
114+ image = rotateImage (image, rotation);
115+ if (image == nil ) {
116+ callback (@[@" Can't rotate the image." , @" " ]);
117+ return ;
118+ }
119+ }
120+
121+ // Do the resizing
122+ UIImage * scaledImage = [image scaleToSize: newSize];
123+ if (scaledImage == nil ) {
124+ callback (@[@" Can't resize the image." , @" " ]);
125+ return ;
126+ }
127+
128+ // Compress and save the image
129+ if (!saveImage (fullPath, scaledImage, format, quality)) {
130+ callback (@[@" Can't save the image. Check your compression format and your output path" , @" " ]);
131+ return ;
132+ }
133+ NSURL *fileUrl = [[NSURL alloc ] initFileURLWithPath: fullPath];
134+ NSString *fileName = fileUrl.lastPathComponent ;
135+ NSError *attributesError = nil ;
136+ NSDictionary *fileAttributes = [[NSFileManager defaultManager ] attributesOfItemAtPath: fullPath error: &attributesError];
137+ NSNumber *fileSize = fileAttributes == nil ? 0 : [fileAttributes objectForKey: NSFileSize ];
138+ NSDictionary *response = @{@" path" : fullPath,
139+ @" uri" : fileUrl.absoluteString ,
140+ @" name" : fileName,
141+ @" size" : fileSize == nil ? @(0 ) : fileSize,
142+ @" width" : @(scaledImage.size .width ),
143+ @" height" : @(scaledImage.size .height )
144+ };
145+
146+ callback (@[[NSNull null ], response]);
147+ }
148+
99149RCT_EXPORT_METHOD (createResizedImage:(NSString *)path
100150 width:(float )width
101151 height:(float )height
@@ -105,73 +155,34 @@ bool saveImage(NSString * fullPath, UIImage * image, NSString * format, float qu
105155 outputPath:(NSString *)outputPath
106156 callback:(RCTResponseSenderBlock)callback)
107157{
108- CGSize newSize = CGSizeMake (width, height);
109-
110- // Set image extension
111- NSString *extension = @" jpg" ;
112- if ([format isEqualToString: @" PNG" ]) {
113- extension = @" png" ;
114- }
158+ dispatch_async (dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0 ), ^{
159+ CGSize newSize = CGSizeMake (width, height);
115160
116-
117- NSString * fullPath;
118- @try {
119- fullPath = generateFilePath (extension, outputPath);
120- } @catch (NSException *exception) {
121- callback (@[@" Invalid output path." , @" " ]);
122- return ;
123- }
161+ // Set image extension
162+ NSString *extension = @" jpg" ;
163+ if ([format isEqualToString: @" PNG" ]) {
164+ extension = @" png" ;
165+ }
124166
125- [[_bridge moduleForClass: [RCTImageLoader class ]] loadImageWithURLRequest: [RCTConvert NSURLRequest: path] callback: ^(NSError *error, UIImage *image) {
126- if (error || image == nil ) {
127- if ([path hasPrefix: @" data:" ] || [path hasPrefix: @" file:" ]) {
128- NSURL *imageUrl = [[NSURL alloc ] initWithString: path];
129- image = [UIImage imageWithData: [NSData dataWithContentsOfURL: imageUrl]];
130- } else {
131- image = [[UIImage alloc ] initWithContentsOfFile: path];
132- }
133- if (image == nil ) {
134- callback (@[@" Can't retrieve the file from the path." , @" " ]);
135- return ;
136- }
167+ NSString * fullPath;
168+ @try {
169+ fullPath = generateFilePath (extension, outputPath);
170+ } @catch (NSException *exception) {
171+ callback (@[@" Invalid output path." , @" " ]);
172+ return ;
137173 }
138174
139- // Rotate image if rotation is specified.
140- if (0 != (int )rotation) {
141- image = rotateImage (image, rotation);
142- if (image == nil ) {
143- callback (@[@" Can't rotate the image." , @" " ]);
175+ [[_bridge moduleForClass: [RCTImageLoader class ]] loadImageWithURLRequest: [RCTConvert NSURLRequest: path] callback: ^(NSError *error, UIImage *image) {
176+ if (error) {
177+ callback (@[@" Can't retrieve the file from the path." , @" " ]);
144178 return ;
145179 }
146- }
147180
148- // Do the resizing
149- UIImage * scaledImage = [image scaleToSize: newSize];
150- if (scaledImage == nil ) {
151- callback (@[@" Can't resize the image." , @" " ]);
152- return ;
153- }
181+ transformImage (image, callback, rotation, newSize, fullPath, format, quality);
182+ }];
154183
155- // Compress and save the image
156- if (!saveImage (fullPath, scaledImage, format, quality)) {
157- callback (@[@" Can't save the image. Check your compression format and your output path" , @" " ]);
158- return ;
159- }
160- NSURL *fileUrl = [[NSURL alloc ] initFileURLWithPath: fullPath];
161- NSString *fileName = fileUrl.lastPathComponent ;
162- NSError *attributesError = nil ;
163- NSDictionary *fileAttributes = [[NSFileManager defaultManager ] attributesOfItemAtPath: fullPath error: &attributesError];
164- NSNumber *fileSize = fileAttributes == nil ? 0 : [fileAttributes objectForKey: NSFileSize ];
165- NSDictionary *response = @{@" path" : fullPath,
166- @" uri" : fileUrl.absoluteString ,
167- @" name" : fileName,
168- @" size" : fileSize == nil ? @(0 ) : fileSize,
169- @" width" : @(scaledImage.size .width ),
170- @" height" : @(scaledImage.size .height )
171- };
172-
173- callback (@[[NSNull null ], response]);
174- }];
184+
185+ });
175186}
176187
177188@end
0 commit comments