├── .gitignore ├── GCResize ├── GCResize.m └── GCResize.xcodeproj │ ├── project.pbxproj │ └── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcuserdata │ └── zach.xcuserdatad │ └── UserInterfaceState.xcuserstate ├── package.json └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | npm-debug.log 3 | -------------------------------------------------------------------------------- /GCResize/GCResize.m: -------------------------------------------------------------------------------- 1 | // 2 | // GCResize.m 3 | // Go Code Foundation 4 | // 5 | // Created by Zach Silveira on 12/2/15. 6 | // 7 | 8 | #import "RCTBridgeModule.h" 9 | @import CoreGraphics; 10 | @import UIKit; 11 | 12 | 13 | 14 | @interface GCResize : NSObject 15 | @end 16 | 17 | @implementation GCResize; 18 | 19 | 20 | RCT_EXPORT_MODULE(); 21 | 22 | //resize the image to the specified dimensions 23 | UIImage *Resize(NSDictionary *input) 24 | { 25 | CGSize size = CGSizeMake([input[@"width"] intValue], [input[@"height"] intValue]); 26 | UIGraphicsBeginImageContext(size); 27 | UIImage *image = [UIImage imageWithContentsOfFile:[input objectForKey:@"image"]]; 28 | 29 | [image drawInRect:CGRectMake(0, 0, size.width, size.height)]; 30 | UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext(); 31 | UIGraphicsEndImageContext(); 32 | return croppedImage; 33 | } 34 | 35 | //save to disk temporarily 36 | NSString *createTempImage(UIImage *image) 37 | { 38 | NSString *tmpDirectory = NSTemporaryDirectory(); 39 | NSString *tempImage = [tmpDirectory stringByAppendingPathComponent:@"temp.png"]; 40 | [UIImagePNGRepresentation(image) writeToFile:tempImage atomically:YES]; 41 | return tempImage; 42 | } 43 | 44 | //image -> resized base64 45 | RCT_EXPORT_METHOD(base64:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) 46 | { 47 | UIImage * croppedImage = Resize(input); 48 | NSString *base64 = [UIImagePNGRepresentation(croppedImage) base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]; 49 | NSString *pngBase64 = [NSString stringWithFormat:@"%s%@", "data:image/png;base64,", base64]; 50 | callback(@[pngBase64]); 51 | } 52 | 53 | 54 | 55 | //image -> cropped image path 56 | RCT_EXPORT_METHOD(path:(NSDictionary *)input callback:(RCTResponseSenderBlock)callback) 57 | { 58 | UIImage *croppedImage = Resize(input); 59 | NSString *tempImagePath = createTempImage(croppedImage); 60 | callback(@[tempImagePath]); 61 | } 62 | @end 63 | -------------------------------------------------------------------------------- /GCResize/GCResize.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXFileReference section */ 10 | 923EE9481C0F77B000DEE59E /* GCResize.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = GCResize.m; sourceTree = ""; }; 11 | /* End PBXFileReference section */ 12 | 13 | /* Begin PBXGroup section */ 14 | 923EE9411C0F779300DEE59E = { 15 | isa = PBXGroup; 16 | children = ( 17 | 923EE9481C0F77B000DEE59E /* GCResize.m */, 18 | ); 19 | sourceTree = ""; 20 | }; 21 | /* End PBXGroup section */ 22 | 23 | /* Begin PBXProject section */ 24 | 923EE9421C0F779300DEE59E /* Project object */ = { 25 | isa = PBXProject; 26 | attributes = { 27 | LastUpgradeCheck = 0710; 28 | }; 29 | buildConfigurationList = 923EE9451C0F779300DEE59E /* Build configuration list for PBXProject "GCResize" */; 30 | compatibilityVersion = "Xcode 3.2"; 31 | developmentRegion = English; 32 | hasScannedForEncodings = 0; 33 | knownRegions = ( 34 | en, 35 | ); 36 | mainGroup = 923EE9411C0F779300DEE59E; 37 | projectDirPath = ""; 38 | projectRoot = ""; 39 | targets = ( 40 | ); 41 | }; 42 | /* End PBXProject section */ 43 | 44 | /* Begin XCBuildConfiguration section */ 45 | 923EE9461C0F779300DEE59E /* Debug */ = { 46 | isa = XCBuildConfiguration; 47 | buildSettings = { 48 | }; 49 | name = Debug; 50 | }; 51 | 923EE9471C0F779300DEE59E /* Release */ = { 52 | isa = XCBuildConfiguration; 53 | buildSettings = { 54 | }; 55 | name = Release; 56 | }; 57 | /* End XCBuildConfiguration section */ 58 | 59 | /* Begin XCConfigurationList section */ 60 | 923EE9451C0F779300DEE59E /* Build configuration list for PBXProject "GCResize" */ = { 61 | isa = XCConfigurationList; 62 | buildConfigurations = ( 63 | 923EE9461C0F779300DEE59E /* Debug */, 64 | 923EE9471C0F779300DEE59E /* Release */, 65 | ); 66 | defaultConfigurationIsVisible = 0; 67 | defaultConfigurationName = Release; 68 | }; 69 | /* End XCConfigurationList section */ 70 | }; 71 | rootObject = 923EE9421C0F779300DEE59E /* Project object */; 72 | } 73 | -------------------------------------------------------------------------------- /GCResize/GCResize.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /GCResize/GCResize.xcodeproj/project.xcworkspace/xcuserdata/zach.xcuserdatad/UserInterfaceState.xcuserstate: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GoCoders/rn-resize/cce7980552f16a17a350df90316baaa7f2b2eea6/GCResize/GCResize.xcodeproj/project.xcworkspace/xcuserdata/zach.xcuserdatad/UserInterfaceState.xcuserstate -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "rn-resize", 3 | "version": "0.0.3", 4 | "description": "resize images in react native", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": " " 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "git+ssh://git@github.com/CodeFoundation/rn-resize.git" 12 | }, 13 | "keywords": [ 14 | "rn", 15 | "react", 16 | "react-native", 17 | "react-component", 18 | "ios" 19 | ], 20 | "author": "Zach Silveira", 21 | "license": "ISC", 22 | "bugs": { 23 | "url": "https://github.com/CodeFoundation/rn-resize/issues" 24 | }, 25 | "homepage": "https://github.com/CodeFoundation/rn-resize#readme" 26 | } 27 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | ##rn-resize 2 | 3 | ###Install 4 | 5 | `npm install rn-resize --save` 6 | 7 | Drag `GCResize.xcodeproj` into your libraries folder and drag `GCResize.m` to `Compile Sources` under `Build Phases`. Follow the [React Native guide](https://facebook.github.io/react-native/docs/linking-libraries-ios.html) on linking libraries if you get stuck. 8 | 9 | ##Description 10 | 11 | Resize images in React Native. Useful if you're using [react-native-camera](https://github.com/lwansbrough/react-native-camera) and want to upload files (using something like [react-native-file-transfer](https://github.com/kamilkp/react-native-file-transfer)) but you don't want the full, insanely huge resolution photos to be uploaded. 12 | 13 | ##Example 14 | 15 | ```js 16 | import { GCResize } from 'NativeModules' 17 | 18 | GCResize.path({ 19 | image: this.props.image, 20 | width: 300, 21 | height: 500 22 | }, resizedImage => this.setState({resizedImage})) 23 | ``` 24 | 25 | `options:` 26 | 27 | The `image` key must be the full path to a photo on a device. 28 | 29 | You can get the full path of the resized image by using `GCResize.path` (see example above) or the same syntax but call `GCResize.base64` to receive a base64 encoded string. Getting the full path is recommended. 30 | --------------------------------------------------------------------------------