diff --git a/plugin.xml b/plugin.xml index a05a8fa..eef118a 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,7 +1,7 @@ + version="0.6.1"> Canvas 2 Image diff --git a/src/ios/Canvas2ImagePlugin.m b/src/ios/Canvas2ImagePlugin.m index 734ee00..15025bc 100644 --- a/src/ios/Canvas2ImagePlugin.m +++ b/src/ios/Canvas2ImagePlugin.m @@ -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 +#import + @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 @@ -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]; }