Skip to content

Commit

Permalink
Switch to promises
Browse files Browse the repository at this point in the history
  • Loading branch information
Michaël Villeneuve committed Jan 25, 2018
1 parent 9b8b188 commit 2195e8d
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.Promise;

public class RNImgToBase64Module extends ReactContextBaseJavaModule {

Expand All @@ -29,13 +29,13 @@ public String getName() {
}

@ReactMethod
public void getBase64String(String uri, Callback callback) {
public void getBase64String(String uri, Promise promise) {
try {
Bitmap image = MediaStore.Images.Media.getBitmap(reactContext.getContentResolver(), Uri.parse(uri));
if (image == null) {
callback.invoke("Failed to decode Bitmap, uri: " + uri);
promise.reject("Error", "Failed to decode Bitmap, uri: " + uri);
} else {
callback.invoke(null, bitmapToBase64(image));
promise.resolve(bitmapToBase64(image));
}
} catch (Error e) {
e.printStackTrace();
Expand Down
7 changes: 5 additions & 2 deletions ios/RNImgToBase64.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,14 @@ - (dispatch_queue_t)methodQueue
RCT_EXPORT_MODULE()

#pragma mark getBase64String
RCT_EXPORT_METHOD(getBase64String:(NSURL*)url callback:(RCTResponseSenderBlock)callback) {
RCT_EXPORT_METHOD(getBase64String:(NSURL*)url
resolve:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject
) {
dispatch_async(dispatch_queue_create("image_processing", 0), ^{
NSData* data = [NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
callback(@[[NSNull null], [data base64EncodedStringWithOptions:0]]);
resolve([data base64EncodedStringWithOptions:0]);
});
});
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

{
"name": "react-native-image-base64",
"version": "0.1.1",
"version": "0.1.2",
"description": "Convert image to base64",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 2195e8d

Please sign in to comment.