diff --git a/README.md b/README.md index be39148..9a2c842 100644 --- a/README.md +++ b/README.md @@ -24,8 +24,10 @@ If you get stuck open an issue. It's the first time I've published react native ### Import module ```javascript -const React = require('react-native'); -const {ReactNativeImageCropping} = React.NativeModules; +import { + NativeModules, +} from 'react-native' +const { ReactNativeImageCropping } = NativeModules; ``` ### Crop the image @@ -38,12 +40,12 @@ It is using RCTImageLoader so it should be able to crop any image that react kno const originalImage = require('CrazyFlowers.jpg'); ReactNativeImageCropping - .cropImageWithUrl(originalImage.uri) + .cropImageWithUrl(originalImage.uri) .then(image => { - //Image is saved in NSTemporaryDirectory! - //image = {uri, width, height} - }, - err => console.log(b)); + //Image is saved in NSTemporaryDirectory! + //image = {uri, width, height} + }) + .catch(err => { console.log(err) }) ``` #### Lock to specific aspect ratio: @@ -63,15 +65,15 @@ Available aspect ratios: Example: ```javascript -let aspectRatio = ReactNativeImageCropping.AspectRatioSquare; +const aspectRatio = ReactNativeImageCropping.AspectRatioSquare; ReactNativeImageCropping .cropImageWithUrlAndAspect(imageUrl, aspectRatio) .then(image => { //Image is saved in NSTemporaryDirectory! //image = {uri, width, height} - }, - err => console.log(b)); + }) + .catch(err => { console.log(err) }) ``` diff --git a/ReactNativeImageCropping.h b/ReactNativeImageCropping.h index 19b67e0..27c42f0 100644 --- a/ReactNativeImageCropping.h +++ b/ReactNativeImageCropping.h @@ -1,4 +1,4 @@ -#import "RCTBridgeModule.h" +#import #import "TOCropView.h" #import diff --git a/ReactNativeImageCropping.m b/ReactNativeImageCropping.m index ebeaf75..a9c04f7 100644 --- a/ReactNativeImageCropping.m +++ b/ReactNativeImageCropping.m @@ -88,8 +88,24 @@ - (NSDictionary *)constantsToExport - (void)handleImageLoad:(UIImage *)image { + // hardcoded downsample of image + float oldWidth = image.size.width; + UIImage *scaledImage = image; + if (oldWidth > 800) { + float scaleFactor = 800 / oldWidth; + + float newHeight = image.size.height * scaleFactor; + float newWidth = oldWidth * scaleFactor; + + UIGraphicsBeginImageContext(CGSizeMake(newWidth, newHeight)); + [image drawInRect:CGRectMake(0, 0, newWidth, newHeight)]; + scaledImage = UIGraphicsGetImageFromCurrentImageContext(); + UIGraphicsEndImageContext(); + + } + UIViewController *root = [[[[UIApplication sharedApplication] delegate] window] rootViewController]; - TOCropViewController *cropViewController = [[TOCropViewController alloc] initWithImage:image]; + TOCropViewController *cropViewController = [[TOCropViewController alloc] initWithImage:scaledImage]; cropViewController.delegate = self; if(self.aspectRatio) { @@ -113,10 +129,10 @@ - (void)cropViewController:(TOCropViewController *)cropViewController didCropToI [cropViewController dismissViewControllerAnimated:YES completion:nil]; }); - NSData *pngData = UIImagePNGRepresentation(image); - NSString *fileName = [NSString stringWithFormat:@"memegenerator-crop-%lf.png", [NSDate timeIntervalSinceReferenceDate]]; - NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName]; //Add the file name) - [pngData writeToFile:filePath atomically:YES]; + NSData *jpgData = UIImageJPEGRepresentation(image, 80); + NSString *fileName = [NSString stringWithFormat:@"resized-%lf.jpg", [NSDate timeIntervalSinceReferenceDate]]; + NSString *filePath = [NSTemporaryDirectory() stringByAppendingPathComponent:fileName]; + [jpgData writeToFile:filePath atomically:YES]; NSNumber *width = [NSNumber numberWithFloat:image.size.width]; NSNumber *height = [NSNumber numberWithFloat:image.size.height]; diff --git a/package.json b/package.json index c4da687..7a6dd71 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-image-cropping", - "version": "1.0.0", + "version": "1.1.5", "keywords": "react-native image cropping", "description": "Simple image cropping library for react-native.", "license": "MIT", @@ -9,6 +9,6 @@ "TOCropViewController": "git+https://github.com/meznaric/TOCropViewController.git" }, "peerDependencies": { - "react-native": "^0.28.0" + "react-native": ">=0.40.0" } }