From 1420b335eb0b2e88da296f737ec3ee5bd85a74d3 Mon Sep 17 00:00:00 2001 From: yao Date: Thu, 18 Feb 2016 18:30:45 +0800 Subject: [PATCH 1/2] Update Canvas2ImagePlugin.h --- src/ios/Canvas2ImagePlugin.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ios/Canvas2ImagePlugin.h b/src/ios/Canvas2ImagePlugin.h index ef7bc56..c97d033 100644 --- a/src/ios/Canvas2ImagePlugin.h +++ b/src/ios/Canvas2ImagePlugin.h @@ -9,6 +9,7 @@ #import +#import "AssetsLibrary/AssetsLibrary.h" @interface Canvas2ImagePlugin : CDVPlugin { From 340b93bc2b200ab62529f3d37983dff2cd2d724e Mon Sep 17 00:00:00 2001 From: yao Date: Thu, 18 Feb 2016 18:34:47 +0800 Subject: [PATCH 2/2] Update Canvas2ImagePlugin.m --- src/ios/Canvas2ImagePlugin.m | 39 ++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 22 deletions(-) diff --git a/src/ios/Canvas2ImagePlugin.m b/src/ios/Canvas2ImagePlugin.m index ad063ae..7689585 100644 --- a/src/ios/Canvas2ImagePlugin.m +++ b/src/ios/Canvas2ImagePlugin.m @@ -22,30 +22,25 @@ @implementation Canvas2ImagePlugin - (void)saveImageDataToLibrary:(CDVInvokedUrlCommand*)command { self.callbackId = command.callbackId; - NSData* imageData = [[NSData alloc] initWithBase64EncodedString:[command.arguments objectAtIndex:0] options:0]; + NSData* imageData = [NSData dataFromBase64String:[command.arguments objectAtIndex:0]]; UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease]; - UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil); - -} - -- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo -{ - // Was there an error? - if (error != NULL) - { - // Show error message... - NSLog(@"ERROR: %@",error); - CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString:error.description]; - [self.commandDelegate sendPluginResult:result callbackId:self.callbackId]; - } - else // No errors - { - // Show message image successfully saved - NSLog(@"IMAGE SAVED!"); - CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:@"Image saved"]; - [self.commandDelegate sendPluginResult:result callbackId:self.callbackId]; - } + ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; + [library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){ + if (error) { + // Show error message... + NSLog(@"ERROR: %@",error); + CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_ERROR messageAsString:error.description]; + [self.webView stringByEvaluatingJavaScriptFromString:[result toErrorCallbackString: self.callbackId]]; + } else { + // Show message image successfully saved + NSLog(@"IMAGE SAVED!"); + NSString* url = [assetURL absoluteString]; + CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:url]; + [self.webView stringByEvaluatingJavaScriptFromString:[result toSuccessCallbackString: self.callbackId]]; + } + }]; + [library release]; } - (void)dealloc