Skip to content

Absolute Path Update #44

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion plugin.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<plugin xmlns="http://www.phonegap.com/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="org.devgeeks.Canvas2ImagePlugin"
version="0.6.0">
version="0.6.1">

<name>Canvas 2 Image</name>

Expand Down
59 changes: 42 additions & 17 deletions src/ios/Canvas2ImagePlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,54 @@
//
// Created by Tommy-Carlos Williams on 29/03/12.
// Copyright (c) 2012 Tommy-Carlos Williams. All rights reserved.
// MIT Licensed
// MIT Licensed
//
// 99% of this code developed by tommy-carlos williams and ThalesValentim

#import "Canvas2ImagePlugin.h"
#import <Cordova/CDV.h>
#import <Foundation/Foundation.h>


@implementation Canvas2ImagePlugin
@synthesize callbackId;

//-(CDVPlugin*) initWithWebView:(UIWebView*)theWebView
//{
// self = (Canvas2ImagePlugin*)[super initWithWebView:theWebView];
// return self;
//}

-(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
if ([[extension lowercaseString] isEqualToString:@"png"]) {
[UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
} else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
[UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
} else {
ALog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);
}
}


- (void)saveImageDataToLibrary:(CDVInvokedUrlCommand*)command
{
NSDate *date = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateFormat:@"ddMMyyyyHHmmss"];
NSString *dateString = [dateFormatter stringFromDate:date];

self.callbackId = command.callbackId;
NSData* imageData = [NSData dataFromBase64String:[command.arguments objectAtIndex:0]];

UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease];
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

NSData* imageData = [NSData dataFromBase64String:[command.arguments objectAtIndex:0]];

UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease];

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *temp = @"ImageFile-";
NSString *ImageName = [temp stringByAppendingFormat:@"%d",dateString];
[self saveImage:image withFileName:ImageName ofType:@"png" inDirectory:path];
NSString *tileDirectory = [[NSBundle mainBundle] resourcePath];
NSString *documentFolderPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSLog(@"Tile Directory: %@", tileDirectory);
NSLog(@"Doc Directory: %@", documentFolderPath);
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:[NSString stringWithFormat:@"%@/%@.png", documentFolderPath, ImageName]];
[self.webView stringByEvaluatingJavaScriptFromString:[result toSuccessCallbackString: self.callbackId]];

}

- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
Expand All @@ -36,21 +61,21 @@ - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error context
{
// Show error message...
NSLog(@"ERROR: %@",error);
CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString:error.description];
[self.webView stringByEvaluatingJavaScriptFromString:[result toErrorCallbackString: self.callbackId]];
CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString:error.description];
[self.webView stringByEvaluatingJavaScriptFromString:[result toErrorCallbackString: self.callbackId]];
}
else // No errors
{
// Show message image successfully saved
NSLog(@"IMAGE SAVED!");
CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:@"Image saved"];
[self.webView stringByEvaluatingJavaScriptFromString:[result toSuccessCallbackString: self.callbackId]];
CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:@"Image saved"];
[self.webView stringByEvaluatingJavaScriptFromString:[result toSuccessCallbackString: self.callbackId]];
}
}

- (void)dealloc
{
[callbackId release];
{
[callbackId release];
[super dealloc];
}

Expand Down