Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 37 additions & 38 deletions Classes/ComSideshowcoderJpgcompressorModule.m
Original file line number Diff line number Diff line change
@@ -1,9 +1,26 @@
/**
* Your Copyright Here
* Copyright (c) 2011, Philipp Fehre All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
* following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of conditions and the following
* disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided with the distribution.

* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Appcelerator Titanium is Copyright (c) 2009-2010 by Appcelerator, Inc.
* and licensed under the Apache Public License (version 2)
*/
**/

#import "ComSideshowcoderJpgcompressorModule.h"
#import "TiBase.h"
#import "TiHost.h"
Expand All @@ -29,59 +46,40 @@ -(NSString*)moduleId

-(void)startup
{
// this method is called when the module is first loaded
// you *must* call the superclass
[super startup];

NSLog(@"[INFO] %@ loaded",self);
}

-(void)shutdown:(id)sender
{
// this method is called when the module is being unloaded
// typically this is during shutdown. make sure you don't do too
// much processing here or the app will be quit forceably

// you *must* call the superclass
[super shutdown:sender];
}

#pragma mark Cleanup
#pragma mark Cleanup

-(void)dealloc
{
// release any resources that have been retained by the module
[super dealloc];
}

#pragma mark Internal Memory Management

-(void)didReceiveMemoryWarning:(NSNotification*)notification
{
// optionally release any resources that can be dynamically
// reloaded once memory is available - such as caches
[super didReceiveMemoryWarning:notification];
}

#pragma mark Listener Notifications

-(void)_listenerAdded:(NSString *)type count:(int)count
{
if (count == 1 && [type isEqualToString:@"my_event"])
{
// the first (of potentially many) listener is being added
// for event named 'my_event'
}
}

-(void)_listenerRemoved:(NSString *)type count:(int)count
{
if (count == 0 && [type isEqualToString:@"my_event"])
{
// the last listener called for event named 'my_event' has
// been removed, we can optionally clean up any resources
// since no body is listening at this point for that event
}
}

#pragma Public APIs
Expand All @@ -96,7 +94,7 @@ -(id)compress:(NSArray*)args
// Get the output path if passed
NSString *imgName = nil;
NSMutableString *path = [NSMutableString string];
NSLog(@"[DEBUG] Args lenght: %u", [args count]);
NSLog(@"[DEBUG] Args lenght: %u", [args count]);
if ([args count] > 1) {
ENSURE_ARG_COUNT(args, 2);
imageData = [[args objectAtIndex:0] data];
Expand All @@ -106,33 +104,35 @@ -(id)compress:(NSArray*)args
} else {
imageData = [[args objectAtIndex:0] data];
}

// DEBUG OUTPUT
NSLog(@"[DEBUG] Received data size: %u Desired size: %@", [imageData length], cs);


UIImage* image = [[UIImage alloc] initWithCIImage:[[args objectAtIndex:0] image]];
// Check if data is larger than desired
if ([imageData length] > [cs intValue]) {
// Calculate the needed compression and check if
CGFloat compressFactor = [cs floatValue] / [[NSNumber numberWithUnsignedInt:[imageData length]] floatValue];
// Calculate the needed compression and check if
CGFloat compressFactor = [cs floatValue] / [[NSNumber numberWithUnsignedLong:[imageData length]] floatValue];
if(compressFactor < [cq floatValue]) {
NSLog(@"[DEBUG] Compress Factor: %f Worst Quality: %f", compressFactor, [cq floatValue]);
NSLog(@"[DEBUG] Compress Factor: %f Worst Quality: %f", compressFactor, [cq floatValue]);
compressFactor = [cq floatValue];
}

if(imgName != nil){
// write to file and return path
[UIImageJPEGRepresentation([[args objectAtIndex:0] image], compressFactor) writeToFile:path atomically:YES];
[UIImageJPEGRepresentation(image, compressFactor) writeToFile:path atomically:YES];
return path;
} else {
// return compressed image
NSData* compressedData = UIImageJPEGRepresentation([[args objectAtIndex:0] image], compressFactor);
NSData* compressedData = UIImageJPEGRepresentation(image, compressFactor);
NSLog(@"[DEBUG] Compressed size: %u Compress Factor: %f", [compressedData length], compressFactor);
return [[[TiBlob alloc] initWithData:compressedData mimetype:@"application/octet-stream"] autorelease];
}
}
// return the uncompressed image
if(imgName != nil){
// write to file and return path
[UIImageJPEGRepresentation([[args objectAtIndex:0] image], 1.0) writeToFile:path atomically:YES];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:path atomically:YES];
return path;
} else {
// return uncompressed image
Expand All @@ -145,7 +145,7 @@ -(id)scale:(id)args
// Storage for the scale size
CGSize newSize;
// Get the values needed from the arguments
UIImage *image = [[args objectAtIndex:0] image];
UIImage* image = [[UIImage alloc] initWithCIImage:[[args objectAtIndex:0] image]];
newSize.width = [[TiUtils numberFromObject:[args objectAtIndex:1]] floatValue];
newSize.height = [[TiUtils numberFromObject:[args objectAtIndex:2]] floatValue];
NSLog(@"[DBEUG] Scale size: %f x %f", newSize.width, newSize.height);
Expand All @@ -160,21 +160,20 @@ -(id)scale:(id)args

-(id)compressSize
{
return [self valueForUndefinedKey:@"compressSize"];
return [self valueForUndefinedKey:@"compressSize"];
}

-(void)setCompressSize:(id)value
{
[self replaceValue:[TiUtils numberFromObject:value]
forKey:@"compressSize"
[self replaceValue:[TiUtils numberFromObject:value]
forKey:@"compressSize"
notification:NO];
}

-(id)worstCompressQuality
{
// when set return value else return 1 as it is 100% quality
if ([self valueForUndefinedKey:@"worstCompressQuality"]) {
return[self valueForUndefinedKey:@"worstCompressQuality"];
return [self valueForUndefinedKey:@"worstCompressQuality"];
}
return [NSNumber numberWithInt:0];
}
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ See `example/app.js` and https://github.com/sideshowcoder/jpgcompressor/blob/mas

Testing
-------
Run the example app and follow it's log see `example/app.js` for more details
Build the module via `python build.py` and install it `unzip -o
com.sideshowcoder.jpgcompressor-iphone-0.5.4.zip -d ~/Library/Application\ Support/Titanium/`


Setup
-----
Expand Down
4 changes: 2 additions & 2 deletions jpgcompressor.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
OTHER_LDFLAGS = "";
PREBINDING = NO;
SDKROOT = iphoneos;
VALID_ARCHS = "armv7 i386 x86_64 arm64";
VALID_ARCHS = "i386 armv7 x86_64 arm64";
};
name = Debug;
};
Expand All @@ -271,7 +271,7 @@
OTHER_LDFLAGS = "";
PREBINDING = NO;
SDKROOT = iphoneos;
VALID_ARCHS = "armv7 i386 x86_64 arm64";
VALID_ARCHS = "i386 armv7 x86_64 arm64";
};
name = Release;
};
Expand Down
4 changes: 2 additions & 2 deletions manifest
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: 0.5.3
version: 0.5.4
description: Compress JPEG Images using UIImageJPEGRepresentation, and scale using UIGraphicsBeginImageContext
author: Philipp Fehre <philipp.fehre@googlemail.com>
license: BSD
Expand All @@ -10,5 +10,5 @@ name: jpgcompressor
moduleid: com.sideshowcoder.jpgcompressor
guid: ff873835-cf92-4d21-b2c0-0f577f0ce8f3
platform: iphone
minsdk: 3.5.0.GA
minsdk: 5.1.1.GA
architectures: armv7 i386 x86_64 arm64
4 changes: 2 additions & 2 deletions titanium.xcconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
// OF YOUR TITANIUM SDK YOU'RE BUILDING FOR
//
//
TITANIUM_SDK_VERSION = 3.5.0.GA
TITANIUM_SDK_VERSION = 5.1.1.GA


//
//
// THESE SHOULD BE OK GENERALLY AS-IS
//
TITANIUM_SDK = ~/Library/Application Support/Titanium/mobilesdk/osx/$(TITANIUM_SDK_VERSION)
Expand Down