Add iOS implementation

This commit is contained in:
Florian Rival 2015-12-02 22:53:06 +01:00
parent 0d4db6341f
commit c57f9d1d05
14 changed files with 797 additions and 7 deletions

21
.gitignore vendored Normal file
View File

@ -0,0 +1,21 @@
node_modules
workbench
*.log
# Xcode
.DS_Store
build/
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
*.xcworkspace
!default.xcworkspace
xcuserdata
profile
*.moved-aside
DerivedData
.idea/

View File

@ -1 +1,6 @@
# RnImageResizer
## iOS
You need to add `RNImageResizer.xcodeproj` to **Libraries** and add `libRNImageResizer.a` to **Link Binary With Libraries** under **Build Phases**. [More info and screenshots about how to do this is available in the React Native documentation](http://facebook.github.io/react-native/docs/linking-libraries-ios.html#content).

View File

@ -0,0 +1,274 @@
// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
};
objectVersion = 46;
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 */; };
/* End PBXBuildFile section */
/* Begin PBXCopyFilesBuildPhase section */
17C2CB641C0F979D00A087DF /* CopyFiles */ = {
isa = PBXCopyFilesBuildPhase;
buildActionMask = 2147483647;
dstPath = "include/$(PRODUCT_NAME)";
dstSubfolderSpec = 16;
files = (
17C2CB6A1C0F979D00A087DF /* RNImageResizer.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 = "<group>"; };
17C2CB6B1C0F979D00A087DF /* RNImageResizer.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = RNImageResizer.m; sourceTree = "<group>"; };
17C2CB881C0F9C0F00A087DF /* ImageHelpers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ImageHelpers.h; sourceTree = "<group>"; };
17C2CB891C0F9C0F00A087DF /* ImageHelpers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ImageHelpers.m; sourceTree = "<group>"; };
17C2CB8A1C0F9C0F00A087DF /* ImageResizerModule.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ImageResizerModule.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
17C2CB631C0F979D00A087DF /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */
/* Begin PBXGroup section */
17C2CB5D1C0F979D00A087DF = {
isa = PBXGroup;
children = (
17C2CB681C0F979D00A087DF /* RNImageResizer */,
17C2CB671C0F979D00A087DF /* Products */,
);
sourceTree = "<group>";
};
17C2CB671C0F979D00A087DF /* Products */ = {
isa = PBXGroup;
children = (
17C2CB661C0F979D00A087DF /* libRNImageResizer.a */,
);
name = Products;
sourceTree = "<group>";
};
17C2CB681C0F979D00A087DF /* RNImageResizer */ = {
isa = PBXGroup;
children = (
17C2CB881C0F9C0F00A087DF /* ImageHelpers.h */,
17C2CB891C0F9C0F00A087DF /* ImageHelpers.m */,
17C2CB8A1C0F9C0F00A087DF /* ImageResizerModule.m */,
17C2CB691C0F979D00A087DF /* RNImageResizer.h */,
17C2CB6B1C0F979D00A087DF /* RNImageResizer.m */,
);
path = RNImageResizer;
sourceTree = "<group>";
};
/* End PBXGroup section */
/* Begin PBXNativeTarget section */
17C2CB651C0F979D00A087DF /* RNImageResizer */ = {
isa = PBXNativeTarget;
buildConfigurationList = 17C2CB6F1C0F979D00A087DF /* Build configuration list for PBXNativeTarget "RNImageResizer" */;
buildPhases = (
17C2CB621C0F979D00A087DF /* Sources */,
17C2CB631C0F979D00A087DF /* Frameworks */,
17C2CB641C0F979D00A087DF /* CopyFiles */,
);
buildRules = (
);
dependencies = (
);
name = RNImageResizer;
productName = RNImageResizer;
productReference = 17C2CB661C0F979D00A087DF /* libRNImageResizer.a */;
productType = "com.apple.product-type.library.static";
};
/* End PBXNativeTarget section */
/* Begin PBXProject section */
17C2CB5E1C0F979D00A087DF /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 0710;
ORGANIZATIONNAME = BAM;
TargetAttributes = {
17C2CB651C0F979D00A087DF = {
CreatedOnToolsVersion = 7.1.1;
};
};
};
buildConfigurationList = 17C2CB611C0F979D00A087DF /* Build configuration list for PBXProject "RNImageResizer" */;
compatibilityVersion = "Xcode 3.2";
developmentRegion = English;
hasScannedForEncodings = 0;
knownRegions = (
en,
);
mainGroup = 17C2CB5D1C0F979D00A087DF;
productRefGroup = 17C2CB671C0F979D00A087DF /* Products */;
projectDirPath = "";
projectRoot = "";
targets = (
17C2CB651C0F979D00A087DF /* RNImageResizer */,
);
};
/* End PBXProject section */
/* Begin PBXSourcesBuildPhase section */
17C2CB621C0F979D00A087DF /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
17C2CB6C1C0F979D00A087DF /* RNImageResizer.m in Sources */,
17C2CB8B1C0F9C0F00A087DF /* ImageHelpers.m in Sources */,
17C2CB8C1C0F9C0F00A087DF /* ImageResizerModule.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */
/* Begin XCBuildConfiguration section */
17C2CB6D1C0F979D00A087DF /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_DYNAMIC_NO_PIC = NO;
GCC_NO_COMMON_BLOCKS = YES;
GCC_OPTIMIZATION_LEVEL = 0;
GCC_PREPROCESSOR_DEFINITIONS = (
"DEBUG=1",
"$(inherited)",
);
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.1;
MTL_ENABLE_DEBUG_INFO = YES;
ONLY_ACTIVE_ARCH = YES;
SDKROOT = iphoneos;
};
name = Debug;
};
17C2CB6E1C0F979D00A087DF /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
ALWAYS_SEARCH_USER_PATHS = NO;
CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x";
CLANG_CXX_LIBRARY = "libc++";
CLANG_ENABLE_MODULES = YES;
CLANG_ENABLE_OBJC_ARC = YES;
CLANG_WARN_BOOL_CONVERSION = YES;
CLANG_WARN_CONSTANT_CONVERSION = YES;
CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR;
CLANG_WARN_EMPTY_BODY = YES;
CLANG_WARN_ENUM_CONVERSION = YES;
CLANG_WARN_INT_CONVERSION = YES;
CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR;
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
COPY_PHASE_STRIP = NO;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
GCC_C_LANGUAGE_STANDARD = gnu99;
GCC_NO_COMMON_BLOCKS = YES;
GCC_WARN_64_TO_32_BIT_CONVERSION = YES;
GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR;
GCC_WARN_UNDECLARED_SELECTOR = YES;
GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE;
GCC_WARN_UNUSED_FUNCTION = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
IPHONEOS_DEPLOYMENT_TARGET = 9.1;
MTL_ENABLE_DEBUG_INFO = NO;
SDKROOT = iphoneos;
VALIDATE_PRODUCT = YES;
};
name = Release;
};
17C2CB701C0F979D00A087DF /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
HEADER_SEARCH_PATHS = (
"$(SRCROOT)/../../React/**",
"$(SRCROOT)/../react-native/React/**",
"$(SRCROOT)/node_modules/react-native/React/**",
);
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
};
name = Debug;
};
17C2CB711C0F979D00A087DF /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
HEADER_SEARCH_PATHS = (
"$(SRCROOT)/../../React/**",
"$(SRCROOT)/../react-native/React/**",
"$(SRCROOT)/node_modules/react-native/React/**",
);
OTHER_LDFLAGS = "-ObjC";
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
};
name = Release;
};
/* End XCBuildConfiguration section */
/* Begin XCConfigurationList section */
17C2CB611C0F979D00A087DF /* Build configuration list for PBXProject "RNImageResizer" */ = {
isa = XCConfigurationList;
buildConfigurations = (
17C2CB6D1C0F979D00A087DF /* Debug */,
17C2CB6E1C0F979D00A087DF /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
17C2CB6F1C0F979D00A087DF /* Build configuration list for PBXNativeTarget "RNImageResizer" */ = {
isa = XCConfigurationList;
buildConfigurations = (
17C2CB701C0F979D00A087DF /* Debug */,
17C2CB711C0F979D00A087DF /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
rootObject = 17C2CB5E1C0F979D00A087DF /* Project object */;
}

View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<Workspace
version = "1.0">
<FileRef
location = "self:RNImageResizer.xcodeproj">
</FileRef>
</Workspace>

View File

@ -0,0 +1,30 @@
{
"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"
}
]
}

View File

@ -0,0 +1,80 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "0710"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "17C2CB651C0F979D00A087DF"
BuildableName = "libRNImageResizer.a"
BlueprintName = "RNImageResizer"
ReferencedContainer = "container:RNImageResizer.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
<AdditionalOptions>
</AdditionalOptions>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "17C2CB651C0F979D00A087DF"
BuildableName = "libRNImageResizer.a"
BlueprintName = "RNImageResizer"
ReferencedContainer = "container:RNImageResizer.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
</AdditionalOptions>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<MacroExpansion>
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "17C2CB651C0F979D00A087DF"
BuildableName = "libRNImageResizer.a"
BlueprintName = "RNImageResizer"
ReferencedContainer = "container:RNImageResizer.xcodeproj">
</BuildableReference>
</MacroExpansion>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>SchemeUserState</key>
<dict>
<key>RNImageResizer.xcscheme</key>
<dict>
<key>orderHint</key>
<integer>0</integer>
</dict>
</dict>
<key>SuppressBuildableAutocreation</key>
<dict>
<key>17C2CB651C0F979D00A087DF</key>
<dict>
<key>primary</key>
<true/>
</dict>
</dict>
</dict>
</plist>

View File

@ -0,0 +1,58 @@
/*
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
"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 <UIKit/UIKit.h>
extern const CGBitmapInfo kDefaultCGBitmapInfo;
extern const CGBitmapInfo kDefaultCGBitmapInfoNoAlpha;
float GetScaleForProportionalResize( CGSize theSize, CGSize intoSize, bool onlyScaleDown, bool maximize );
CGContextRef CreateCGBitmapContextForWidthAndHeight( unsigned int width, unsigned int height, CGColorSpaceRef optionalColorSpace, CGBitmapInfo optionalInfo );
CGImageRef CreateCGImageFromUIImageScaled( UIImage* inImage, float scaleFactor );
@interface UIImage (scale)
-(UIImage*)scaleToSize:(CGSize)toSize;
@end

View File

@ -0,0 +1,180 @@
/*
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

View File

@ -0,0 +1,83 @@
//
// ImageResize.m
// ChoozItApp
//
// Created by Florian Rival on 19/11/15.
//
#import <AssetsLibrary/AssetsLibrary.h>
#import "RCTBridgeModule.h"
#include "ImageHelpers.h"
@interface ImageResizerModule : NSObject <RCTBridgeModule>
@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) {
// Here, we have the asset, let's retrieve the image from it
CGImageRef imgRef = [[asset defaultRepresentation] fullResolutionImage];
UIImage * image = [UIImage imageWithCGImage:imgRef];
//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

View File

@ -0,0 +1,13 @@
//
// RNImageResizer.h
// RNImageResizer
//
// Created by Florian Rival on 02/12/15.
// Copyright © 2015 BAM. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface RNImageResizer : NSObject
@end

View File

@ -0,0 +1,13 @@
//
// RNImageResizer.m
// RNImageResizer
//
// Created by Florian Rival on 02/12/15.
// Copyright © 2015 BAM. All rights reserved.
//
#import "RNImageResizer.h"
@implementation RNImageResizer
@end

View File

@ -1,8 +1,7 @@
{
"name": "rn-image-resizer",
"name": "react-native-image-resizer",
"version": "0.0.1",
"description": "Resize local images with React Native",
"main": "index.android.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
@ -10,17 +9,22 @@
"react-native",
"react",
"android",
"camera",
"camera-roll"
"ios",
"images",
"image",
"image-resizer",
"scaling",
"scale",
"resize"
],
"repository": {
"type": "git",
"url": "git+https://github.com/4ian/rn-image-resizer.git"
"url": "git+https://github.com/4ian/react-native-image-resizer.git"
},
"author": "Florian Rival <florianr@bamlab.fr> (http://bamlab.fr)",
"license": "MIT",
"bugs": {
"url": "https://github.com/4ian/rn-image-resizer/issues"
"url": "https://github.com/4ian/react-native-image-resizer/issues"
},
"homepage": "https://github.com/4ian/rn-image-resizer#readme"
"homepage": "https://github.com/4ian/react-native-image-resizer#readme"
}