Skip to content
This repository has been archived by the owner on Mar 12, 2019. It is now read-only.

Commit

Permalink
Merge pull request #16 from stromgren/ios-path-fix
Browse files Browse the repository at this point in the history
Fix for iOS not finding the image when using URL
  • Loading branch information
RaananW committed Apr 8, 2015
2 parents ee3ad88 + f168a5c commit f9d6d65
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<engines>
<engine name="cordova" version=">=3.0.0" />
</engines>

<dependency id="org.apache.cordova.file" version="1.0.1" />

<js-module src="www/imageresize.js" name="ImageResizePlugin">
<clobbers target="plugins.imageResizer" />
Expand Down
Binary file added src/.DS_Store
Binary file not shown.
55 changes: 36 additions & 19 deletions src/ios/ImageResize.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ImageResizer PhoneGap / Cordova Plugin
//
// Created by Raanan Weber on 02.01.12.
//
//
// The software is open source, MIT Licensed.
// Copyright (c) 2012-2013 webXells GmbH , http://www.webxells.com. All rights reserved.
//
Expand All @@ -15,26 +15,27 @@
#import "ImageResize.h"
#import "UIImage+Scale.h"
#import "NSData+Base64.h"
#import "CDVFile.h"

@implementation ImageResize

@synthesize callbackID;

- (void) resizeImage:(CDVInvokedUrlCommand*)command {
NSDictionary *options = [command.arguments objectAtIndex:0];
CGFloat width = [[options objectForKey:@"width"] floatValue];

CGFloat width = [[options objectForKey:@"width"] floatValue];
CGFloat height = [[options objectForKey:@"height"] floatValue];
NSInteger quality = [[options objectForKey:@"quality"] integerValue];
NSInteger quality = [[options objectForKey:@"quality"] integerValue];
NSString *format = [options objectForKey:@"format"];
NSString *resizeType = [options objectForKey:@"resizeType"];
bool storeImage = [[options objectForKey:@"storeImage"] boolValue];
NSString *filename = [options objectForKey:@"filename"];
bool accountForPixelDensity = [[options objectForKey:@"pixelDensity"] boolValue];

//Load the image
UIImage *img = [self getImageUsingOptions:options];

UIImage *img = [self getImageUsingOptions:options];
UIImage *scaledImage = nil;
CGFloat newHeight;
CGFloat newWidth;
Expand Down Expand Up @@ -70,7 +71,7 @@ - (void) resizeImage:(CDVInvokedUrlCommand*)command {
newWidth = img.size.width * scaleFactor;
newHeight = img.size.height * scaleFactor;
}

//Double size for retina if option set to true
if (accountForPixelDensity && [[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2.0) {
if (img.size.width > newWidth * 2 && img.size.height > newHeight * 2) {
Expand All @@ -81,11 +82,11 @@ - (void) resizeImage:(CDVInvokedUrlCommand*)command {
newHeight = img.size.height;
}
}

scaledImage = [img scaleToSize:CGSizeMake(newWidth, newHeight)];
NSNumber *newWidthObj = [[NSNumber alloc] initWithFloat:newWidth];
NSNumber *newHeightObj = [[NSNumber alloc] initWithFloat:newHeight];

CDVPluginResult* pluginResult = nil;
if (storeImage) {
bool written = [self saveImage:scaledImage withOptions:options];
Expand All @@ -102,10 +103,10 @@ - (void) resizeImage:(CDVInvokedUrlCommand*)command {
} else {
imageDataObject = UIImagePNGRepresentation(scaledImage);
}

NSString *encodedString = [imageDataObject base64EncodingWithLineLength:0];
NSDictionary* result = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:encodedString, newWidthObj, newHeightObj, nil] forKeys:[NSArray arrayWithObjects: @"imageData", @"width", @"height", nil]];

if (encodedString != nil) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:result];
} else {
Expand All @@ -125,21 +126,37 @@ - (UIImage*) getImageUsingOptions:(NSDictionary*)options {
if([imageDataType isEqualToString:@"base64Image"]==YES) {
img = [[UIImage alloc] initWithData:[NSData dataWithBase64EncodedString:imageData]];
} else {
img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL fileURLWithPath:imageData]]];
img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfFile:[self getUrl:imageData]]];
}
return img;
}

- (NSString *)getUrl:(NSString *)urlString
{
NSString *path = nil;
id filePlugin = [self.commandDelegate getCommandInstance:@"File"];
if (filePlugin != nil) {
CDVFilesystemURL* url = [CDVFilesystemURL fileSystemURLWithString:urlString];
path = [filePlugin filesystemPathForURL:url];
}
if (path == nil) {
if ([urlString hasPrefix:@"file:"]) {
path = [[NSURL URLWithString:urlString] path];
}
}
return path;
}

- (void) imageSize:(CDVInvokedUrlCommand*)command {
NSDictionary *options = [command.arguments objectAtIndex:0];

UIImage * img = [self getImageUsingOptions:options];
UIImage * img = [self getImageUsingOptions:options];
NSNumber *width = [[NSNumber alloc] initWithInt:img.size.width];
NSNumber *height = [[NSNumber alloc] initWithInt:img.size.height];
NSDictionary* dic = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:width,height,nil] forKeys:[NSArray arrayWithObjects: @"width", @"height", nil]];

CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:dic];

[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}

Expand All @@ -153,7 +170,7 @@ - (bool) saveImage:(UIImage *)img withOptions:(NSDictionary *) options {
NSString *format = [options objectForKey:@"format"];
NSString *filename = [options objectForKey:@"filename"];
NSString *directory = [options objectForKey:@"directory"];
NSInteger quality = [[options objectForKey:@"quality"] integerValue];
NSInteger quality = [[options objectForKey:@"quality"] integerValue];
bool photoAlbum = [[options objectForKey:@"photoAlbum"] boolValue];
if (photoAlbum == YES) {
UIImageWriteToSavedPhotosAlbum(img, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil);
Expand All @@ -179,7 +196,7 @@ - (bool) saveImage:(UIImage *)img withOptions:(NSDictionary *) options {
} else {
fullFileName = [NSMutableString stringWithString: documentsDirectory];
}

[fullFileName appendString:@"/"];
[fullFileName appendString:filename];
NSRange r = [filename rangeOfString:format options:NSCaseInsensitiveSearch];
Expand All @@ -190,7 +207,7 @@ - (bool) saveImage:(UIImage *)img withOptions:(NSDictionary *) options {
NSError *error = nil;
bool written = [imageDataObject writeToFile:fullFileName options:NSDataWritingAtomic error:&error];
if (!written) {
NSLog(@"Write returned error: %@", [error localizedDescription]);
NSLog(@"Write returned error: %@", [error localizedDescription]);
}
return written;
}
Expand All @@ -207,7 +224,7 @@ - (void) imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSEr
title = NSLocalizedString(@"Error", @"");
message = [error description];
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title
message:message
delegate:nil
cancelButtonTitle:@"OK"
Expand Down

0 comments on commit f9d6d65

Please sign in to comment.