From cfdc6a9b5c0e1b3dd55724d854682ba996988579 Mon Sep 17 00:00:00 2001 From: unknown Date: Tue, 13 Jan 2015 23:23:08 +0100 Subject: [PATCH] Replaced the use of UIImageWriteToSavedPhotosAlbum with the function writeImageToSavedPhotosAlbum from the library ALAssetsLibrary The aim of this change is to be able to return the image URL to the success callback --- src/ios/Canvas2ImagePlugin.h | 1 + src/ios/Canvas2ImagePlugin.m | 44 +++++++++++++++++------------------- 2 files changed, 22 insertions(+), 23 deletions(-) diff --git a/src/ios/Canvas2ImagePlugin.h b/src/ios/Canvas2ImagePlugin.h index ef7bc56..cb064b9 100644 --- a/src/ios/Canvas2ImagePlugin.h +++ b/src/ios/Canvas2ImagePlugin.h @@ -9,6 +9,7 @@ #import +#import @interface Canvas2ImagePlugin : CDVPlugin { diff --git a/src/ios/Canvas2ImagePlugin.m b/src/ios/Canvas2ImagePlugin.m index 734ee00..bb1a434 100644 --- a/src/ios/Canvas2ImagePlugin.m +++ b/src/ios/Canvas2ImagePlugin.m @@ -6,6 +6,7 @@ // Copyright (c) 2012 Tommy-Carlos Williams. All rights reserved. // MIT Licensed // +//Updated to return file path in success callback #import "Canvas2ImagePlugin.h" #import @@ -24,30 +25,27 @@ - (void)saveImageDataToLibrary:(CDVInvokedUrlCommand*)command 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); - + UIImage* image = [[[UIImage alloc] initWithData:imageData] autorelease]; + ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; + + [library writeImageToSavedPhotosAlbum: image.CGImage metadata:nil 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(@"Saved URL : %@", assetURL); + CDVPluginResult* result = [CDVPluginResult resultWithStatus: CDVCommandStatus_OK messageAsString:[assetURL absoluteString]]; + [self.webView stringByEvaluatingJavaScriptFromString:[result toSuccessCallbackString: self.callbackId]]; + } +}]; +[library release]; } - -- (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.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]]; - } -} - - (void)dealloc { [callbackId release];