Created
September 8, 2015 16:42
-
-
Save dhrrgn/f51e743fa576f7173f1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RNImagePicker.h | |
// | |
// Created by Dan Horrigan on 9/8/15. | |
// Copyright (c) 2015 Tackk, Inc. All rights reserved. | |
// | |
#import "RCTBridgeModule.h" | |
#import <UIKit/UIKit.h> | |
@interface RNImagePicker : NSObject <RCTBridgeModule, UINavigationControllerDelegate, UIImagePickerControllerDelegate> | |
- (void)showImagePicker:(UIImagePickerControllerSourceType)sourceType; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RNImagePicker.m | |
// | |
// Created by Dan Horrigan on 9/8/15. | |
// Copyright (c) 2015 Tackk, Inc. All rights reserved. | |
// | |
#import "RNImagePicker.h" | |
#define JPEG_QUALITY 0.7 | |
@interface RNImagePicker () | |
@property (nonatomic, strong) UIImagePickerController *imagePicker; | |
@property (nonatomic, strong) RCTResponseSenderBlock callback; | |
@end | |
@implementation RNImagePicker | |
RCT_EXPORT_MODULE(); | |
RCT_EXPORT_METHOD(fromCamera:(RCTResponseSenderBlock)callback) | |
{ | |
#if TARGET_IPHONE_SIMULATOR | |
NSLog(@"Camera not available on simulator"); | |
return; | |
#endif | |
self.callback = callback; | |
[self showImagePicker:UIImagePickerControllerSourceTypeCamera]; | |
} | |
RCT_EXPORT_METHOD(fromLibrary:(RCTResponseSenderBlock)callback) | |
{ | |
#if TARGET_IPHONE_SIMULATOR | |
NSLog(@"Camera not available on simulator"); | |
return; | |
#endif | |
self.callback = callback; | |
[self showImagePicker:UIImagePickerControllerSourceTypePhotoLibrary]; | |
} | |
- (void)showImagePicker:(UIImagePickerControllerSourceType)sourceType | |
{ | |
self.imagePicker = [[UIImagePickerController alloc] init]; | |
self.imagePicker.modalPresentationStyle = UIModalPresentationFullScreen; | |
self.imagePicker.delegate = self; | |
self.imagePicker.sourceType = sourceType; | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
UIViewController *vc = [[[UIApplication sharedApplication] keyWindow] rootViewController]; | |
while (vc.presentedViewController) { | |
vc = vc.presentedViewController; | |
} | |
[vc presentViewController:self.imagePicker animated:YES completion:nil]; | |
}); | |
} | |
- (void)imagePickerController:(UIImagePickerController *)imagePicker didFinishPickingMediaWithInfo:(NSDictionary *)info | |
{ | |
// Get rid of the view controller right away. | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
[imagePicker dismissViewControllerAnimated:YES completion:nil]; | |
}); | |
UIImage *image = info[UIImagePickerControllerOriginalImage]; | |
NSData *imageData = UIImageJPEGRepresentation(image, JPEG_QUALITY); | |
NSString *dataString = [imageData base64EncodedStringWithOptions:0]; | |
self.callback(@[@"data", dataString]); | |
} | |
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)imagePicker | |
{ | |
dispatch_async(dispatch_get_main_queue(), ^{ | |
[imagePicker dismissViewControllerAnimated:YES completion:nil]; | |
}); | |
self.callback(@[@"cancel"]); | |
} | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react-native'; | |
var { | |
NativeModules: { | |
RNImagePicker, | |
}, | |
} = React; | |
// Take a new photo | |
RNImagePicker.fromCamera((type, response) => { | |
// response is a base64 image. | |
if (type === "data" && response) { | |
console.log('Got Photo'); | |
} else { | |
// No photo | |
console.log('No Photo'); | |
} | |
}); | |
// Import from the Library | |
RNImagePicker.fromLibrary((type, response) => { | |
// response is a base64 image. | |
if (type === "data" && response) { | |
console.log('Got Photo'); | |
} else { | |
// No photo | |
console.log('No Photo'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment