diff --git a/RNImageResizer.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/RNImageResizer.xcodeproj/project.xcworkspace/contents.xcworkspacedata deleted file mode 100644 index 0553590..0000000 --- a/RNImageResizer.xcodeproj/project.xcworkspace/contents.xcworkspacedata +++ /dev/null @@ -1,7 +0,0 @@ - - - - - diff --git a/RNImageResizer.xcodeproj/project.xcworkspace/xcshareddata/RNImageResizer.xcscmblueprint b/RNImageResizer.xcodeproj/project.xcworkspace/xcshareddata/RNImageResizer.xcscmblueprint deleted file mode 100644 index 999c841..0000000 --- a/RNImageResizer.xcodeproj/project.xcworkspace/xcshareddata/RNImageResizer.xcscmblueprint +++ /dev/null @@ -1,30 +0,0 @@ -{ - "DVTSourceControlWorkspaceBlueprintPrimaryRemoteRepositoryKey" : "0D109C37DD8764732D9148B2C1B21C765B61E85B", - "DVTSourceControlWorkspaceBlueprintWorkingCopyRepositoryLocationsKey" : { - - }, - "DVTSourceControlWorkspaceBlueprintWorkingCopyStatesKey" : { - "0D109C37DD8764732D9148B2C1B21C765B61E85B" : 0, - "44AB18E9006B644ECA9DE92549E1AE9E7494A12E" : 0 - }, - "DVTSourceControlWorkspaceBlueprintIdentifierKey" : "40E1EF49-C975-4DF2-98BD-DECB0E0D09CA", - "DVTSourceControlWorkspaceBlueprintWorkingCopyPathsKey" : { - "0D109C37DD8764732D9148B2C1B21C765B61E85B" : "react-native-image-resizer\/", - "44AB18E9006B644ECA9DE92549E1AE9E7494A12E" : ".." - }, - "DVTSourceControlWorkspaceBlueprintNameKey" : "RNImageResizer", - "DVTSourceControlWorkspaceBlueprintVersion" : 204, - "DVTSourceControlWorkspaceBlueprintRelativePathToProjectKey" : "RNImageResizer.xcodeproj", - "DVTSourceControlWorkspaceBlueprintRemoteRepositoriesKey" : [ - { - "DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/bamlab\/react-native-image-resizer", - "DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git", - "DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "0D109C37DD8764732D9148B2C1B21C765B61E85B" - }, - { - "DVTSourceControlWorkspaceBlueprintRemoteRepositoryURLKey" : "https:\/\/github.com\/bamlab\/chooz-it-app", - "DVTSourceControlWorkspaceBlueprintRemoteRepositorySystemKey" : "com.apple.dt.Xcode.sourcecontrol.Git", - "DVTSourceControlWorkspaceBlueprintRemoteRepositoryIdentifierKey" : "44AB18E9006B644ECA9DE92549E1AE9E7494A12E" - } - ] -} \ No newline at end of file diff --git a/RNImageResizer.xcodeproj/project.xcworkspace/xcuserdata/florian.xcuserdatad/UserInterfaceState.xcuserstate b/RNImageResizer.xcodeproj/project.xcworkspace/xcuserdata/florian.xcuserdatad/UserInterfaceState.xcuserstate deleted file mode 100644 index f2c4507..0000000 Binary files a/RNImageResizer.xcodeproj/project.xcworkspace/xcuserdata/florian.xcuserdatad/UserInterfaceState.xcuserstate and /dev/null differ diff --git a/RNImageResizer.xcodeproj/xcuserdata/florian.xcuserdatad/xcschemes/RNImageResizer.xcscheme b/RNImageResizer.xcodeproj/xcuserdata/florian.xcuserdatad/xcschemes/RNImageResizer.xcscheme deleted file mode 100644 index e0ea4e5..0000000 --- a/RNImageResizer.xcodeproj/xcuserdata/florian.xcuserdatad/xcschemes/RNImageResizer.xcscheme +++ /dev/null @@ -1,80 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/RNImageResizer.xcodeproj/xcuserdata/florian.xcuserdatad/xcschemes/xcschememanagement.plist b/RNImageResizer.xcodeproj/xcuserdata/florian.xcuserdatad/xcschemes/xcschememanagement.plist deleted file mode 100644 index 296d7a5..0000000 --- a/RNImageResizer.xcodeproj/xcuserdata/florian.xcuserdatad/xcschemes/xcschememanagement.plist +++ /dev/null @@ -1,22 +0,0 @@ - - - - - SchemeUserState - - RNImageResizer.xcscheme - - orderHint - 0 - - - SuppressBuildableAutocreation - - 17C2CB651C0F979D00A087DF - - primary - - - - - diff --git a/RNImageResizer/ImageHelpers.m b/RNImageResizer/ImageHelpers.m deleted file mode 100644 index 9fc52d2..0000000 --- a/RNImageResizer/ImageHelpers.m +++ /dev/null @@ -1,180 +0,0 @@ -/* - File: ImageHelpers.m - - Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple - Inc. ("Apple") in consideration of your agreement to the following - terms, and your use, installation, modification or redistribution of - this Apple software constitutes acceptance of these terms. If you do - not agree with these terms, please do not use, install, modify or - redistribute this Apple software. - - In consideration of your agreement to abide by the following terms, and - subject to these terms, Apple grants you a personal, non-exclusive - license, under Apple's copyrights in this original Apple software (the - "Apple Software"), to use, reproduce, modify and redistribute the Apple - Software, with or without modifications, in source and/or binary forms; - provided that if you redistribute the Apple Software in its entirety and - without modifications, you must retain this notice and the following - text and disclaimers in all such redistributions of the Apple Software. - Neither the name, trademarks, service marks or logos of Apple Inc. may - be used to endorse or promote products derived from the Apple Software - without specific prior written permission from Apple. Except as - expressly stated in this notice, no other rights or licenses, express or - implied, are granted by Apple herein, including but not limited to any - patent rights that may be infringed by your derivative works or by other - works in which the Apple Software may be incorporated. - - The Apple Software is provided by Apple on an "AS IS" basis. APPLE - MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION - THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS - FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND - OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. - - IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL - OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, - MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED - AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), - STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. - - Copyright (C) 2009 Apple Inc. All Rights Reserved. - */ - -#include "ImageHelpers.h" - -const CGBitmapInfo kDefaultCGBitmapInfo = (kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host); -const CGBitmapInfo kDefaultCGBitmapInfoNoAlpha = (kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host); - -CGColorSpaceRef GetDeviceRGBColorSpace() { - static CGColorSpaceRef deviceRGBSpace = NULL; - if( deviceRGBSpace == NULL ) - deviceRGBSpace = CGColorSpaceCreateDeviceRGB(); - return deviceRGBSpace; -} - -float GetScaleForProportionalResize( CGSize theSize, CGSize intoSize, bool onlyScaleDown, bool maximize ) -{ - float sx = theSize.width; - float sy = theSize.height; - float dx = intoSize.width; - float dy = intoSize.height; - float scale = 1; - - if( sx != 0 && sy != 0 ) - { - dx = dx / sx; - dy = dy / sy; - - // if maximize is true, take LARGER of the scales, else smaller - if( maximize ) scale = (dx > dy) ? dx : dy; - else scale = (dx < dy) ? dx : dy; - - if( scale > 1 && onlyScaleDown ) // reset scale - scale = 1; - } - else - { - scale = 0; - } - return scale; -} - -CGContextRef CreateCGBitmapContextForWidthAndHeight( unsigned int width, unsigned int height, - CGColorSpaceRef optionalColorSpace, CGBitmapInfo optionalInfo ) -{ - CGColorSpaceRef colorSpace = (optionalColorSpace == NULL) ? GetDeviceRGBColorSpace() : optionalColorSpace; - CGBitmapInfo alphaInfo = ( (int32_t)optionalInfo < 0 ) ? kDefaultCGBitmapInfo : optionalInfo; - return CGBitmapContextCreate( NULL, width, height, 8, 0, colorSpace, alphaInfo ); -} - -CGImageRef CreateCGImageFromUIImageScaled( UIImage* image, float scaleFactor ) -{ - CGImageRef newImage = NULL; - CGContextRef bmContext = NULL; - BOOL mustTransform = YES; - CGAffineTransform transform = CGAffineTransformIdentity; - UIImageOrientation orientation = image.imageOrientation; - - CGImageRef srcCGImage = CGImageRetain( image.CGImage ); - - size_t width = CGImageGetWidth(srcCGImage) * scaleFactor; - size_t height = CGImageGetHeight(srcCGImage) * scaleFactor; - - // These Orientations are rotated 0 or 180 degrees, so they retain the width/height of the image - if( (orientation == UIImageOrientationUp) || (orientation == UIImageOrientationDown) || (orientation == UIImageOrientationUpMirrored) || (orientation == UIImageOrientationDownMirrored) ) - { - bmContext = CreateCGBitmapContextForWidthAndHeight( width, height, NULL, kDefaultCGBitmapInfo ); - } - else // The other Orientations are rotated ±90 degrees, so they swap width & height. - { - bmContext = CreateCGBitmapContextForWidthAndHeight( height, width, NULL, kDefaultCGBitmapInfo ); - } - - //CGContextSetInterpolationQuality( bmContext, kCGInterpolationLow ); - CGContextSetBlendMode( bmContext, kCGBlendModeCopy ); // we just want to copy the data - - switch(orientation) - { - case UIImageOrientationDown: // 0th row is at the bottom, and 0th column is on the right - Rotate 180 degrees - transform = CGAffineTransformMake(-1.0, 0.0, 0.0, -1.0, width, height); - break; - - case UIImageOrientationLeft: // 0th row is on the left, and 0th column is the bottom - Rotate -90 degrees - transform = CGAffineTransformMake(0.0, 1.0, -1.0, 0.0, height, 0.0); - break; - - case UIImageOrientationRight: // 0th row is on the right, and 0th column is the top - Rotate 90 degrees - transform = CGAffineTransformMake(0.0, -1.0, 1.0, 0.0, 0.0, width); - break; - - case UIImageOrientationUpMirrored: // 0th row is at the top, and 0th column is on the right - Flip Horizontal - transform = CGAffineTransformMake(-1.0, 0.0, 0.0, 1.0, width, 0.0); - break; - - case UIImageOrientationDownMirrored: // 0th row is at the bottom, and 0th column is on the left - Flip Vertical - transform = CGAffineTransformMake(1.0, 0.0, 0, -1.0, 0.0, height); - break; - - case UIImageOrientationLeftMirrored: // 0th row is on the left, and 0th column is the top - Rotate -90 degrees and Flip Vertical - transform = CGAffineTransformMake(0.0, -1.0, -1.0, 0.0, height, width); - break; - - case UIImageOrientationRightMirrored: // 0th row is on the right, and 0th column is the bottom - Rotate 90 degrees and Flip Vertical - transform = CGAffineTransformMake(0.0, 1.0, 1.0, 0.0, 0.0, 0.0); - break; - - default: - mustTransform = NO; - break; - } - - if( mustTransform ) CGContextConcatCTM( bmContext, transform ); - - CGContextDrawImage( bmContext, CGRectMake(0.0, 0.0, width, height), srcCGImage ); - CGImageRelease( srcCGImage ); - newImage = CGBitmapContextCreateImage( bmContext ); - CFRelease( bmContext ); - - return newImage; -} - -@implementation UIImage (scale) - --(UIImage*) scaleToSize:(CGSize)toSize -{ - UIImage *scaledImg = nil; - float scale = GetScaleForProportionalResize( self.size, toSize, false, false ); - CGImageRef cgImage = CreateCGImageFromUIImageScaled( self, scale ); - - if( cgImage ) - { - scaledImg = [UIImage imageWithCGImage:cgImage]; // autoreleased - CGImageRelease( cgImage ); - } - return scaledImg; -} - -@end - diff --git a/RNImageResizer/ImageResizerModule.m b/RNImageResizer/ImageResizerModule.m deleted file mode 100644 index e637187..0000000 --- a/RNImageResizer/ImageResizerModule.m +++ /dev/null @@ -1,92 +0,0 @@ -// -// ImageResize.m -// ChoozItApp -// -// Created by Florian Rival on 19/11/15. -// - -#import -#import "RCTBridgeModule.h" -#include "ImageHelpers.h" - -@interface ImageResizerModule : NSObject -@end - -@implementation ImageResizerModule - -RCT_EXPORT_MODULE(); - -void saveImage(NSString * fullPath, UIImage * image, float quality) -{ - NSData* data = UIImageJPEGRepresentation(image, quality / 100.0); - NSFileManager* fileManager = [NSFileManager defaultManager]; - [fileManager createFileAtPath:fullPath contents:data attributes:nil]; -} - -NSString * generateCacheFilePath(NSString * ext) -{ - NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); - NSString* cacheDirectory = [paths firstObject]; - NSString* name = [[NSUUID UUID] UUIDString]; - NSString* fullName = [NSString stringWithFormat:@"%@.%@", name, ext]; - NSString* fullPath = [cacheDirectory stringByAppendingPathComponent:fullName]; - - return fullPath; -} - -RCT_EXPORT_METHOD(createResizedImage:(NSString *)path width:(float)width height:(float)height quality:(float)quality callback:(RCTResponseSenderBlock)callback) -{ - CGSize newSize = CGSizeMake(width, height); - NSString* fullPath = generateCacheFilePath(@".jpg"); - - NSURL* url = [NSURL URLWithString:path]; - if ([[url scheme] isEqualToString:@"assets-library"]) { - ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init]; - - // Ask for the "Asset" for the URL. An asset is a representation of an image in the Photo application. - [library assetForURL:url resultBlock:^(ALAsset *asset) { - ALAssetRepresentation* representation = [asset defaultRepresentation]; - - // Retrieve the image orientation from the ALAsset - UIImageOrientation orientation = UIImageOrientationUp; - NSNumber* orientationValue = [asset valueForProperty:@"ALAssetPropertyOrientation"]; - if (orientationValue != nil) { - orientation = [orientationValue intValue]; - } - - CGFloat scale = 1; - UIImage* image = [UIImage imageWithCGImage:[representation fullResolutionImage] - scale:scale orientation:orientation]; - - //Do the resizing - UIImage * scaledImage = [image scaleToSize:newSize]; - if (scaledImage == nil) { - callback(@[@"Can't resize the image.", @""]); - return; - } - - saveImage(fullPath, scaledImage, quality); - callback(@[[NSNull null], fullPath]); - } failureBlock:^(NSError *error) { - callback(@[@"Can't retrieve the file from assets library.", @""]); - }]; - } else { - //Get the image from its path - UIImage* image = [[UIImage alloc] initWithContentsOfFile:path]; - if (image == nil) { - callback(@[@"Can't retrieve the file from the path.", @""]); - return; - } - - //Do the resizing - UIImage * scaledImage = [image scaleToSize:newSize]; - if (scaledImage == nil) { - callback(@[@"Can't resize the image.", @""]); - return; - } - - saveImage(fullPath, scaledImage, quality); - callback(@[[NSNull null], fullPath]); - } -} -@end diff --git a/RNImageResizer/RNImageResizer.h b/RNImageResizer/RNImageResizer.h deleted file mode 100644 index f250681..0000000 --- a/RNImageResizer/RNImageResizer.h +++ /dev/null @@ -1,13 +0,0 @@ -// -// RNImageResizer.h -// RNImageResizer -// -// Created by Florian Rival on 02/12/15. -// Copyright © 2015 BAM. All rights reserved. -// - -#import - -@interface RNImageResizer : NSObject - -@end diff --git a/RNImageResizer/RNImageResizer.m b/RNImageResizer/RNImageResizer.m deleted file mode 100644 index c86d84b..0000000 --- a/RNImageResizer/RNImageResizer.m +++ /dev/null @@ -1,13 +0,0 @@ -// -// RNImageResizer.m -// RNImageResizer -// -// Created by Florian Rival on 02/12/15. -// Copyright © 2015 BAM. All rights reserved. -// - -#import "RNImageResizer.h" - -@implementation RNImageResizer - -@end diff --git a/index.ios.js b/index.ios.js index e4b16b6..04fa4c7 100644 --- a/index.ios.js +++ b/index.ios.js @@ -9,7 +9,7 @@ export default { } return new Promise((resolve, reject) => { - NativeModules.ImageResizerModule.createResizedImage(path, width, height, quality, (err, resizedPath) => { + NativeModules.ImageResizer.createResizedImage(path, width, height, quality, (err, resizedPath) => { if (err) { return reject(err); } diff --git a/RNImageResizer.xcodeproj/project.pbxproj b/ios/RCTImageResizer.xcodeproj/project.pbxproj similarity index 58% rename from RNImageResizer.xcodeproj/project.pbxproj rename to ios/RCTImageResizer.xcodeproj/project.pbxproj index 77accad..b414408 100644 --- a/RNImageResizer.xcodeproj/project.pbxproj +++ b/ios/RCTImageResizer.xcodeproj/project.pbxproj @@ -7,36 +7,34 @@ objects = { /* Begin PBXBuildFile section */ - 17C2CB6A1C0F979D00A087DF /* RNImageResizer.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 17C2CB691C0F979D00A087DF /* RNImageResizer.h */; }; - 17C2CB6C1C0F979D00A087DF /* RNImageResizer.m in Sources */ = {isa = PBXBuildFile; fileRef = 17C2CB6B1C0F979D00A087DF /* RNImageResizer.m */; }; - 17C2CB8B1C0F9C0F00A087DF /* ImageHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = 17C2CB891C0F9C0F00A087DF /* ImageHelpers.m */; }; - 17C2CB8C1C0F9C0F00A087DF /* ImageResizerModule.m in Sources */ = {isa = PBXBuildFile; fileRef = 17C2CB8A1C0F9C0F00A087DF /* ImageResizerModule.m */; }; + 5D72D2EC1C16249000E22EC1 /* RCTImageResizer.h in CopyFiles */ = {isa = PBXBuildFile; fileRef = 5D72D2EB1C16249000E22EC1 /* RCTImageResizer.h */; }; + 5D72D2EE1C16249000E22EC1 /* RCTImageResizer.m in Sources */ = {isa = PBXBuildFile; fileRef = 5D72D2ED1C16249000E22EC1 /* RCTImageResizer.m */; }; + B62D1C4C1C95C21300AD70F2 /* ImageHelpers.m in Sources */ = {isa = PBXBuildFile; fileRef = B62D1C4B1C95C21300AD70F2 /* ImageHelpers.m */; }; /* End PBXBuildFile section */ /* Begin PBXCopyFilesBuildPhase section */ - 17C2CB641C0F979D00A087DF /* CopyFiles */ = { + 5D72D2E61C16249000E22EC1 /* CopyFiles */ = { isa = PBXCopyFilesBuildPhase; buildActionMask = 2147483647; dstPath = "include/$(PRODUCT_NAME)"; dstSubfolderSpec = 16; files = ( - 17C2CB6A1C0F979D00A087DF /* RNImageResizer.h in CopyFiles */, + 5D72D2EC1C16249000E22EC1 /* RCTImageResizer.h in CopyFiles */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXCopyFilesBuildPhase section */ /* Begin PBXFileReference section */ - 17C2CB661C0F979D00A087DF /* libRNImageResizer.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRNImageResizer.a; sourceTree = BUILT_PRODUCTS_DIR; }; - 17C2CB691C0F979D00A087DF /* RNImageResizer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RNImageResizer.h; sourceTree = ""; }; - 17C2CB6B1C0F979D00A087DF /* RNImageResizer.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNImageResizer.m; sourceTree = ""; }; - 17C2CB881C0F9C0F00A087DF /* ImageHelpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ImageHelpers.h; sourceTree = ""; }; - 17C2CB891C0F9C0F00A087DF /* ImageHelpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ImageHelpers.m; sourceTree = ""; }; - 17C2CB8A1C0F9C0F00A087DF /* ImageResizerModule.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ImageResizerModule.m; sourceTree = ""; }; + 5D72D2E81C16249000E22EC1 /* libRCTImageResizer.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = libRCTImageResizer.a; sourceTree = BUILT_PRODUCTS_DIR; }; + 5D72D2EB1C16249000E22EC1 /* RCTImageResizer.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RCTImageResizer.h; sourceTree = ""; }; + 5D72D2ED1C16249000E22EC1 /* RCTImageResizer.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RCTImageResizer.m; sourceTree = ""; }; + B62D1C4A1C95C21300AD70F2 /* ImageHelpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ImageHelpers.h; sourceTree = ""; }; + B62D1C4B1C95C21300AD70F2 /* ImageHelpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ImageHelpers.m; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ - 17C2CB631C0F979D00A087DF /* Frameworks */ = { + 5D72D2E51C16249000E22EC1 /* Frameworks */ = { isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( @@ -46,100 +44,98 @@ /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ - 17C2CB5D1C0F979D00A087DF = { + 5D72D2DF1C16249000E22EC1 = { isa = PBXGroup; children = ( - 17C2CB681C0F979D00A087DF /* RNImageResizer */, - 17C2CB671C0F979D00A087DF /* Products */, + 5D72D2EA1C16249000E22EC1 /* RCTImageResizer */, + 5D72D2E91C16249000E22EC1 /* Products */, ); sourceTree = ""; }; - 17C2CB671C0F979D00A087DF /* Products */ = { + 5D72D2E91C16249000E22EC1 /* Products */ = { isa = PBXGroup; children = ( - 17C2CB661C0F979D00A087DF /* libRNImageResizer.a */, + 5D72D2E81C16249000E22EC1 /* libRCTImageResizer.a */, ); name = Products; sourceTree = ""; }; - 17C2CB681C0F979D00A087DF /* RNImageResizer */ = { + 5D72D2EA1C16249000E22EC1 /* RCTImageResizer */ = { isa = PBXGroup; children = ( - 17C2CB881C0F9C0F00A087DF /* ImageHelpers.h */, - 17C2CB891C0F9C0F00A087DF /* ImageHelpers.m */, - 17C2CB8A1C0F9C0F00A087DF /* ImageResizerModule.m */, - 17C2CB691C0F979D00A087DF /* RNImageResizer.h */, - 17C2CB6B1C0F979D00A087DF /* RNImageResizer.m */, + 5D72D2EB1C16249000E22EC1 /* RCTImageResizer.h */, + 5D72D2ED1C16249000E22EC1 /* RCTImageResizer.m */, + B62D1C4A1C95C21300AD70F2 /* ImageHelpers.h */, + B62D1C4B1C95C21300AD70F2 /* ImageHelpers.m */, ); - path = RNImageResizer; + path = RCTImageResizer; sourceTree = ""; }; /* End PBXGroup section */ /* Begin PBXNativeTarget section */ - 17C2CB651C0F979D00A087DF /* RNImageResizer */ = { + 5D72D2E71C16249000E22EC1 /* RCTImageResizer */ = { isa = PBXNativeTarget; - buildConfigurationList = 17C2CB6F1C0F979D00A087DF /* Build configuration list for PBXNativeTarget "RNImageResizer" */; + buildConfigurationList = 5D72D2F11C16249000E22EC1 /* Build configuration list for PBXNativeTarget "RCTImageResizer" */; buildPhases = ( - 17C2CB621C0F979D00A087DF /* Sources */, - 17C2CB631C0F979D00A087DF /* Frameworks */, - 17C2CB641C0F979D00A087DF /* CopyFiles */, + 5D72D2E41C16249000E22EC1 /* Sources */, + 5D72D2E51C16249000E22EC1 /* Frameworks */, + 5D72D2E61C16249000E22EC1 /* CopyFiles */, ); buildRules = ( ); dependencies = ( ); - name = RNImageResizer; - productName = RNImageResizer; - productReference = 17C2CB661C0F979D00A087DF /* libRNImageResizer.a */; + name = RCTImageResizer; + productName = RCTImageResizer; + productReference = 5D72D2E81C16249000E22EC1 /* libRCTImageResizer.a */; productType = "com.apple.product-type.library.static"; }; /* End PBXNativeTarget section */ /* Begin PBXProject section */ - 17C2CB5E1C0F979D00A087DF /* Project object */ = { + 5D72D2E01C16249000E22EC1 /* Project object */ = { isa = PBXProject; attributes = { LastUpgradeCheck = 0710; - ORGANIZATIONNAME = BAM; + ORGANIZATIONNAME = "Atticus White"; TargetAttributes = { - 17C2CB651C0F979D00A087DF = { + 5D72D2E71C16249000E22EC1 = { CreatedOnToolsVersion = 7.1.1; }; }; }; - buildConfigurationList = 17C2CB611C0F979D00A087DF /* Build configuration list for PBXProject "RNImageResizer" */; + buildConfigurationList = 5D72D2E31C16249000E22EC1 /* Build configuration list for PBXProject "RCTImageResizer" */; compatibilityVersion = "Xcode 3.2"; developmentRegion = English; hasScannedForEncodings = 0; knownRegions = ( en, ); - mainGroup = 17C2CB5D1C0F979D00A087DF; - productRefGroup = 17C2CB671C0F979D00A087DF /* Products */; + mainGroup = 5D72D2DF1C16249000E22EC1; + productRefGroup = 5D72D2E91C16249000E22EC1 /* Products */; projectDirPath = ""; projectRoot = ""; targets = ( - 17C2CB651C0F979D00A087DF /* RNImageResizer */, + 5D72D2E71C16249000E22EC1 /* RCTImageResizer */, ); }; /* End PBXProject section */ /* Begin PBXSourcesBuildPhase section */ - 17C2CB621C0F979D00A087DF /* Sources */ = { + 5D72D2E41C16249000E22EC1 /* Sources */ = { isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 17C2CB6C1C0F979D00A087DF /* RNImageResizer.m in Sources */, - 17C2CB8B1C0F9C0F00A087DF /* ImageHelpers.m in Sources */, - 17C2CB8C1C0F9C0F00A087DF /* ImageResizerModule.m in Sources */, + 5D72D2EE1C16249000E22EC1 /* RCTImageResizer.m in Sources */, + B62D1C4C1C95C21300AD70F2 /* ImageHelpers.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; /* End PBXSourcesBuildPhase section */ /* Begin XCBuildConfiguration section */ - 17C2CB6D1C0F979D00A087DF /* Debug */ = { + 5D72D2EF1C16249000E22EC1 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; @@ -182,7 +178,7 @@ }; name = Debug; }; - 17C2CB6E1C0F979D00A087DF /* Release */ = { + 5D72D2F01C16249000E22EC1 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { ALWAYS_SEARCH_USER_PATHS = NO; @@ -219,13 +215,14 @@ }; name = Release; }; - 17C2CB701C0F979D00A087DF /* Debug */ = { + 5D72D2F21C16249000E22EC1 /* Debug */ = { isa = XCBuildConfiguration; buildSettings = { HEADER_SEARCH_PATHS = ( - "$(SRCROOT)/../../React/**", - "$(SRCROOT)/../react-native/React/**", - "$(SRCROOT)/node_modules/react-native/React/**", + "$(inherited)", + /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include, + "$(SRCROOT)/../../react-native/React/**", + "$(SRCROOT)/../../react-native/Libraries/**", ); OTHER_LDFLAGS = "-ObjC"; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -233,13 +230,14 @@ }; name = Debug; }; - 17C2CB711C0F979D00A087DF /* Release */ = { + 5D72D2F31C16249000E22EC1 /* Release */ = { isa = XCBuildConfiguration; buildSettings = { HEADER_SEARCH_PATHS = ( - "$(SRCROOT)/../../React/**", - "$(SRCROOT)/../react-native/React/**", - "$(SRCROOT)/node_modules/react-native/React/**", + "$(inherited)", + /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include, + "$(SRCROOT)/../../react-native/React/**", + "$(SRCROOT)/../../react-native/Libraries/**", ); OTHER_LDFLAGS = "-ObjC"; PRODUCT_NAME = "$(TARGET_NAME)"; @@ -250,25 +248,25 @@ /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ - 17C2CB611C0F979D00A087DF /* Build configuration list for PBXProject "RNImageResizer" */ = { + 5D72D2E31C16249000E22EC1 /* Build configuration list for PBXProject "RCTImageResizer" */ = { isa = XCConfigurationList; buildConfigurations = ( - 17C2CB6D1C0F979D00A087DF /* Debug */, - 17C2CB6E1C0F979D00A087DF /* Release */, + 5D72D2EF1C16249000E22EC1 /* Debug */, + 5D72D2F01C16249000E22EC1 /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; - 17C2CB6F1C0F979D00A087DF /* Build configuration list for PBXNativeTarget "RNImageResizer" */ = { + 5D72D2F11C16249000E22EC1 /* Build configuration list for PBXNativeTarget "RCTImageResizer" */ = { isa = XCConfigurationList; buildConfigurations = ( - 17C2CB701C0F979D00A087DF /* Debug */, - 17C2CB711C0F979D00A087DF /* Release */, + 5D72D2F21C16249000E22EC1 /* Debug */, + 5D72D2F31C16249000E22EC1 /* Release */, ); defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; /* End XCConfigurationList section */ }; - rootObject = 17C2CB5E1C0F979D00A087DF /* Project object */; + rootObject = 5D72D2E01C16249000E22EC1 /* Project object */; } diff --git a/RNImageResizer/ImageHelpers.h b/ios/RCTImageResizer/ImageHelpers.h similarity index 99% rename from RNImageResizer/ImageHelpers.h rename to ios/RCTImageResizer/ImageHelpers.h index ef3ed18..467c614 100644 --- a/RNImageResizer/ImageHelpers.h +++ b/ios/RCTImageResizer/ImageHelpers.h @@ -1,13 +1,13 @@ /* File: ImageHelpers.h - + Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Inc. ("Apple") in consideration of your agreement to the following terms, and your use, installation, modification or redistribution of this Apple software constitutes acceptance of these terms. If you do not agree with these terms, please do not use, install, modify or redistribute this Apple software. - + In consideration of your agreement to abide by the following terms, and subject to these terms, Apple grants you a personal, non-exclusive license, under Apple's copyrights in this original Apple software (the @@ -23,13 +23,13 @@ implied, are granted by Apple herein, including but not limited to any patent rights that may be infringed by your derivative works or by other works in which the Apple Software may be incorporated. - + The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. - + IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS @@ -38,7 +38,7 @@ AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - + Copyright (C) 2009 Apple Inc. All Rights Reserved. */ @@ -55,4 +55,3 @@ CGImageRef CreateCGImageFromUIImageScaled( UIImage* inImage, float scaleFactor @interface UIImage (scale) -(UIImage*)scaleToSize:(CGSize)toSize; @end - diff --git a/ios/RCTImageResizer/ImageHelpers.m b/ios/RCTImageResizer/ImageHelpers.m new file mode 100644 index 0000000..d9e5a19 --- /dev/null +++ b/ios/RCTImageResizer/ImageHelpers.m @@ -0,0 +1,179 @@ +/* + File: ImageHelpers.m + + Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple + Inc. ("Apple") in consideration of your agreement to the following + terms, and your use, installation, modification or redistribution of + this Apple software constitutes acceptance of these terms. If you do + not agree with these terms, please do not use, install, modify or + redistribute this Apple software. + + In consideration of your agreement to abide by the following terms, and + subject to these terms, Apple grants you a personal, non-exclusive + license, under Apple's copyrights in this original Apple software (the + "Apple Software"), to use, reproduce, modify and redistribute the Apple + Software, with or without modifications, in source and/or binary forms; + provided that if you redistribute the Apple Software in its entirety and + without modifications, you must retain this notice and the following + text and disclaimers in all such redistributions of the Apple Software. + Neither the name, trademarks, service marks or logos of Apple Inc. may + be used to endorse or promote products derived from the Apple Software + without specific prior written permission from Apple. Except as + expressly stated in this notice, no other rights or licenses, express or + implied, are granted by Apple herein, including but not limited to any + patent rights that may be infringed by your derivative works or by other + works in which the Apple Software may be incorporated. + + The Apple Software is provided by Apple on an "AS IS" basis. APPLE + MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION + THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS + FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND + OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS. + + IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL + OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, + MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED + AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), + STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + + Copyright (C) 2009 Apple Inc. All Rights Reserved. + */ + +#include "ImageHelpers.h" + +const CGBitmapInfo kDefaultCGBitmapInfo = (kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Host); +const CGBitmapInfo kDefaultCGBitmapInfoNoAlpha = (kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Host); + +CGColorSpaceRef GetDeviceRGBColorSpace() { + static CGColorSpaceRef deviceRGBSpace = NULL; + if( deviceRGBSpace == NULL ) + deviceRGBSpace = CGColorSpaceCreateDeviceRGB(); + return deviceRGBSpace; +} + +float GetScaleForProportionalResize( CGSize theSize, CGSize intoSize, bool onlyScaleDown, bool maximize ) +{ + float sx = theSize.width; + float sy = theSize.height; + float dx = intoSize.width; + float dy = intoSize.height; + float scale = 1; + + if( sx != 0 && sy != 0 ) + { + dx = dx / sx; + dy = dy / sy; + + // if maximize is true, take LARGER of the scales, else smaller + if( maximize ) scale = (dx > dy) ? dx : dy; + else scale = (dx < dy) ? dx : dy; + + if( scale > 1 && onlyScaleDown ) // reset scale + scale = 1; + } + else + { + scale = 0; + } + return scale; +} + +CGContextRef CreateCGBitmapContextForWidthAndHeight( unsigned int width, unsigned int height, + CGColorSpaceRef optionalColorSpace, CGBitmapInfo optionalInfo ) +{ + CGColorSpaceRef colorSpace = (optionalColorSpace == NULL) ? GetDeviceRGBColorSpace() : optionalColorSpace; + CGBitmapInfo alphaInfo = ( (int32_t)optionalInfo < 0 ) ? kDefaultCGBitmapInfo : optionalInfo; + return CGBitmapContextCreate( NULL, width, height, 8, 0, colorSpace, alphaInfo ); +} + +CGImageRef CreateCGImageFromUIImageScaled( UIImage* image, float scaleFactor ) +{ + CGImageRef newImage = NULL; + CGContextRef bmContext = NULL; + BOOL mustTransform = YES; + CGAffineTransform transform = CGAffineTransformIdentity; + UIImageOrientation orientation = image.imageOrientation; + + CGImageRef srcCGImage = CGImageRetain( image.CGImage ); + + size_t width = CGImageGetWidth(srcCGImage) * scaleFactor; + size_t height = CGImageGetHeight(srcCGImage) * scaleFactor; + + // These Orientations are rotated 0 or 180 degrees, so they retain the width/height of the image + if( (orientation == UIImageOrientationUp) || (orientation == UIImageOrientationDown) || (orientation == UIImageOrientationUpMirrored) || (orientation == UIImageOrientationDownMirrored) ) + { + bmContext = CreateCGBitmapContextForWidthAndHeight( width, height, NULL, kDefaultCGBitmapInfo ); + } + else // The other Orientations are rotated ±90 degrees, so they swap width & height. + { + bmContext = CreateCGBitmapContextForWidthAndHeight( height, width, NULL, kDefaultCGBitmapInfo ); + } + + //CGContextSetInterpolationQuality( bmContext, kCGInterpolationLow ); + CGContextSetBlendMode( bmContext, kCGBlendModeCopy ); // we just want to copy the data + + switch(orientation) + { + case UIImageOrientationDown: // 0th row is at the bottom, and 0th column is on the right - Rotate 180 degrees + transform = CGAffineTransformMake(-1.0, 0.0, 0.0, -1.0, width, height); + break; + + case UIImageOrientationLeft: // 0th row is on the left, and 0th column is the bottom - Rotate -90 degrees + transform = CGAffineTransformMake(0.0, 1.0, -1.0, 0.0, height, 0.0); + break; + + case UIImageOrientationRight: // 0th row is on the right, and 0th column is the top - Rotate 90 degrees + transform = CGAffineTransformMake(0.0, -1.0, 1.0, 0.0, 0.0, width); + break; + + case UIImageOrientationUpMirrored: // 0th row is at the top, and 0th column is on the right - Flip Horizontal + transform = CGAffineTransformMake(-1.0, 0.0, 0.0, 1.0, width, 0.0); + break; + + case UIImageOrientationDownMirrored: // 0th row is at the bottom, and 0th column is on the left - Flip Vertical + transform = CGAffineTransformMake(1.0, 0.0, 0, -1.0, 0.0, height); + break; + + case UIImageOrientationLeftMirrored: // 0th row is on the left, and 0th column is the top - Rotate -90 degrees and Flip Vertical + transform = CGAffineTransformMake(0.0, -1.0, -1.0, 0.0, height, width); + break; + + case UIImageOrientationRightMirrored: // 0th row is on the right, and 0th column is the bottom - Rotate 90 degrees and Flip Vertical + transform = CGAffineTransformMake(0.0, 1.0, 1.0, 0.0, 0.0, 0.0); + break; + + default: + mustTransform = NO; + break; + } + + if( mustTransform ) CGContextConcatCTM( bmContext, transform ); + + CGContextDrawImage( bmContext, CGRectMake(0.0, 0.0, width, height), srcCGImage ); + CGImageRelease( srcCGImage ); + newImage = CGBitmapContextCreateImage( bmContext ); + CFRelease( bmContext ); + + return newImage; +} + +@implementation UIImage (scale) + +-(UIImage*) scaleToSize:(CGSize)toSize +{ + UIImage *scaledImg = nil; + float scale = GetScaleForProportionalResize( self.size, toSize, false, false ); + CGImageRef cgImage = CreateCGImageFromUIImageScaled( self, scale ); + + if( cgImage ) + { + scaledImg = [UIImage imageWithCGImage:cgImage]; // autoreleased + CGImageRelease( cgImage ); + } + return scaledImg; +} + +@end diff --git a/ios/RCTImageResizer/RCTImageResizer.h b/ios/RCTImageResizer/RCTImageResizer.h new file mode 100644 index 0000000..db6639c --- /dev/null +++ b/ios/RCTImageResizer/RCTImageResizer.h @@ -0,0 +1,5 @@ +#import "RCTBridge.h" + +@interface ImageResizer : NSObject + +@end diff --git a/ios/RCTImageResizer/RCTImageResizer.m b/ios/RCTImageResizer/RCTImageResizer.m new file mode 100644 index 0000000..89fcfc5 --- /dev/null +++ b/ios/RCTImageResizer/RCTImageResizer.m @@ -0,0 +1,66 @@ +// +// ImageResize.m +// ChoozItApp +// +// Created by Florian Rival on 19/11/15. +// + +#include "RCTImageResizer.h" +#include "ImageHelpers.h" +#import "RCTImageLoader.h" + +@implementation ImageResizer + +@synthesize bridge = _bridge; + +RCT_EXPORT_MODULE(); + +void saveImage(NSString * fullPath, UIImage * image, float quality) +{ + NSData* data = UIImageJPEGRepresentation(image, quality / 100.0); + NSFileManager* fileManager = [NSFileManager defaultManager]; + [fileManager createFileAtPath:fullPath contents:data attributes:nil]; +} + +NSString * generateCacheFilePath(NSString * ext) +{ + NSArray* paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); + NSString* cacheDirectory = [paths firstObject]; + NSString* name = [[NSUUID UUID] UUIDString]; + NSString* fullName = [NSString stringWithFormat:@"%@.%@", name, ext]; + NSString* fullPath = [cacheDirectory stringByAppendingPathComponent:fullName]; + + return fullPath; +} + +RCT_EXPORT_METHOD(createResizedImage:(NSString *)path + width:(float)width + height:(float)height + quality:(float)quality + callback:(RCTResponseSenderBlock)callback) +{ + CGSize newSize = CGSizeMake(width, height); + NSString* fullPath = generateCacheFilePath(@".jpg"); + + [_bridge.imageLoader loadImageWithTag:path callback:^(NSError *error, UIImage *image) { + if (error || image == nil) { + image = [[UIImage alloc] initWithContentsOfFile:path]; + if (image == nil) { + callback(@[@"Can't retrieve the file from the path.", @""]); + return; + } + } + + // Do the resizing + UIImage * scaledImage = [image scaleToSize:newSize]; + if (scaledImage == nil) { + callback(@[@"Can't resize the image.", @""]); + return; + } + + saveImage(fullPath, scaledImage, quality); + callback(@[[NSNull null], fullPath]); + }]; +} + +@end