cordova plugin for saving base64 image
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Base64ImageSaverPlugin.m 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. //
  2. // Base64ImageSaverPlugin.m
  3. // Base64ImageSaverPlugin PhoneGap/Cordova plugin
  4. //
  5. // Created by Tommy-Carlos Williams on 29/03/12.
  6. // Copyright (c) 2012 Tommy-Carlos Williams. All rights reserved.
  7. // MIT Licensed
  8. //
  9. #import "Base64ImageSaverPlugin.h"
  10. #import <Cordova/CDV.h>
  11. @implementation Base64ImageSaverPlugin
  12. @synthesize callbackId;
  13. //-(CDVPlugin*) initWithWebView:(UIWebView*)theWebView
  14. //{
  15. // self = (Base64ImageSaverPlugin*)[super initWithWebView:theWebView];
  16. // return self;
  17. //}
  18. - (void)saveImageDataToLibrary:(CDVInvokedUrlCommand*)command
  19. {
  20. self.callbackId = command.callbackId;
  21. NSData *imageData = [[NSData alloc] initWithBase64EncodedString:[command.arguments objectAtIndex:0] options:nil];
  22. UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease];
  23. UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
  24. }
  25. - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
  26. {
  27. // Was there an error?
  28. if (error != NULL)
  29. {
  30. // Show error message...
  31. NSLog(@"ERROR: %@",error);
  32. CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString:error.description];
  33. // [self.webView stringByEvaluatingJavaScriptFromString:[result toErrorCallbackString: self.callbackId]];
  34. [self.commandDelegate sendPluginResult:result callbackId: self.callbackId];
  35. }
  36. else // No errors
  37. {
  38. // Show message image successfully saved
  39. NSLog(@"IMAGE SAVED!");
  40. CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:@"gallery"];
  41. // [self.webView stringByEvaluatingJavaScriptFromString:[result toSuccessCallbackString: self.callbackId]];
  42. [self.commandDelegate sendPluginResult:result callbackId: self.callbackId];
  43. }
  44. }
  45. - (void)dealloc
  46. {
  47. [callbackId release];
  48. [super dealloc];
  49. }
  50. @end