Skip to content

Commit 5bced19

Browse files
takameyerTrancever
authored andcommitted
feat: iOS - determine the output image type from the input image type (#44)
1 parent 685996d commit 5bced19

File tree

1 file changed

+17
-4
lines changed

1 file changed

+17
-4
lines changed

ios/RNCImageEditor.m

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ @implementation RNCImageEditor
4949
[RCTConvert CGPoint:cropData[@"offset"]],
5050
[RCTConvert CGSize:cropData[@"size"]]
5151
};
52+
NSURL *url = [imageRequest URL];
53+
NSString *urlPath = [url path];
54+
NSString *extension = [urlPath pathExtension];
5255

5356
[_bridge.imageLoader loadImageWithURLRequest:imageRequest callback:^(NSError *error, UIImage *image) {
5457
if (error) {
@@ -72,15 +75,25 @@ @implementation RNCImageEditor
7275
}
7376

7477
// Store image
75-
NSString *path = [RNCFileSystem generatePathInDirectory:[[RNCFileSystem cacheDirectoryPath] stringByAppendingPathComponent:@"ReactNative_cropped_image_"] withExtension:@".jpg"];
78+
NSString *path = NULL;
79+
NSData *imageData = NULL;
80+
81+
if([extension isEqualToString:@"png"]){
82+
imageData = UIImagePNGRepresentation(croppedImage);
83+
path = [RNCFileSystem generatePathInDirectory:[[RNCFileSystem cacheDirectoryPath] stringByAppendingPathComponent:@"ReactNative_cropped_image_"] withExtension:@".png"];
84+
}
85+
else{
86+
87+
imageData = UIImageJPEGRepresentation(croppedImage, 1);
88+
path = [RNCFileSystem generatePathInDirectory:[[RNCFileSystem cacheDirectoryPath] stringByAppendingPathComponent:@"ReactNative_cropped_image_"] withExtension:@".jpg"];
89+
}
7690

77-
NSData *imageData = UIImagePNGRepresentation(croppedImage);
7891
NSError *writeError;
7992
NSString *uri = [RNCImageUtils writeImage:imageData toPath:path error:&writeError];
8093

8194
if (writeError != nil) {
82-
reject(@(writeError.code).stringValue, writeError.description, writeError);
83-
return;
95+
reject(@(writeError.code).stringValue, writeError.description, writeError);
96+
return;
8497
}
8598

8699
resolve(uri);

0 commit comments

Comments
 (0)