Skip to content

Commit 4ce8d89

Browse files
committed
feature: Upon success, return more image information
1 parent 5e91a92 commit 4ce8d89

File tree

3 files changed

+60
-5
lines changed

3 files changed

+60
-5
lines changed

android/src/main/java/com/reactnativecommunity/imageeditor/ImageEditorModuleImpl.kt

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ import android.util.Base64 as AndroidUtilBase64
2323
import androidx.exifinterface.media.ExifInterface
2424
import com.facebook.common.logging.FLog
2525
import com.facebook.infer.annotation.Assertions
26+
import com.facebook.react.bridge.Arguments
2627
import com.facebook.react.bridge.JSApplicationIllegalArgumentException
2728
import com.facebook.react.bridge.Promise
2829
import com.facebook.react.bridge.ReactApplicationContext
2930
import com.facebook.react.bridge.ReadableMap
31+
import com.facebook.react.bridge.WritableMap
3032
import com.facebook.react.common.ReactConstants
3133
import java.io.ByteArrayInputStream
3234
import java.io.File
@@ -162,7 +164,7 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) {
162164
if (mimeType == MimeType.JPEG) {
163165
copyExif(reactContext, Uri.parse(uri), tempFile)
164166
}
165-
promise.resolve(Uri.fromFile(tempFile).toString())
167+
promise.resolve(getResultMap(tempFile, cropped))
166168
} catch (e: Exception) {
167169
promise.reject(e)
168170
}
@@ -437,6 +439,17 @@ class ImageEditorModuleImpl(private val reactContext: ReactApplicationContext) {
437439
)
438440

439441
// Utils
442+
private fun getResultMap(resizedImage: File, image: Bitmap): WritableMap {
443+
val response = Arguments.createMap()
444+
response.putString("path", resizedImage.absolutePath)
445+
response.putString("uri", Uri.fromFile(resizedImage).toString())
446+
response.putString("name", resizedImage.name)
447+
response.putInt("size", resizedImage.length().toInt())
448+
response.putInt("width", image.width)
449+
response.putInt("height", image.height)
450+
return response
451+
}
452+
440453
private fun getMimeType(outOptions: BitmapFactory.Options, format: String?): String {
441454
val mimeType =
442455
when (format) {

ios/RNCImageEditor.mm

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ - (void) cropImage:(NSString *)uri
122122
path = [RNCFileSystem generatePathInDirectory:[[RNCFileSystem cacheDirectoryPath] stringByAppendingPathComponent:@"ReactNative_cropped_image_"] withExtension:@".png"];
123123
}
124124
else{
125-
126125
imageData = UIImageJPEGRepresentation(croppedImage, compressionQuality);
127126
path = [RNCFileSystem generatePathInDirectory:[[RNCFileSystem cacheDirectoryPath] stringByAppendingPathComponent:@"ReactNative_cropped_image_"] withExtension:@".jpg"];
128127
}
@@ -135,7 +134,21 @@ - (void) cropImage:(NSString *)uri
135134
return;
136135
}
137136

138-
resolve(uri);
137+
NSURL *fileurl = [[NSURL alloc] initFileURLWithPath:path];
138+
NSString *filename = fileurl.lastPathComponent;
139+
NSError *attributesError = nil;
140+
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:&attributesError];
141+
NSNumber *fileSize = fileAttributes == nil ? 0 : [fileAttributes objectForKey:NSFileSize];
142+
NSDictionary *response = @{
143+
@"path": path,
144+
@"uri": uri,
145+
@"name": filename,
146+
@"size": fileSize ?: @(0),
147+
@"width": @(croppedImage.size.width),
148+
@"height": @(croppedImage.size.height),
149+
};
150+
151+
resolve(response);
139152
}];
140153
}
141154

src/NativeRNCImageEditor.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import type { TurboModule } from 'react-native';
2-
import type { Double, Float } from 'react-native/Libraries/Types/CodegenTypes';
2+
import type {
3+
Double,
4+
Float,
5+
Int32,
6+
} from 'react-native/Libraries/Types/CodegenTypes';
37
import { TurboModuleRegistry } from 'react-native';
48

59
export interface Spec extends TurboModule {
@@ -46,7 +50,32 @@ export interface Spec extends TurboModule {
4650
*/
4751
format?: string;
4852
}
49-
): Promise<string>;
53+
): Promise<{
54+
/**
55+
* The path to the image file (example: '/data/user/0/.../image.jpg')
56+
*/
57+
path: string;
58+
/**
59+
* The URI of the image (example: 'file:///data/user/0/.../image.jpg')
60+
*/
61+
uri: string;
62+
/**
63+
* The name of the image file. (example: 'image.jpg')
64+
*/
65+
name: string;
66+
/**
67+
* The width of the image in pixels
68+
*/
69+
width: Int32;
70+
/**
71+
* The height of the image in pixels
72+
*/
73+
height: Int32;
74+
/**
75+
* The size of the image in bytes
76+
*/
77+
size: Int32;
78+
}>;
5079
}
5180

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

0 commit comments

Comments
 (0)