Skip to content
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

Add saveDataStringAsImageDataToLibrary api, input datastring as parameter. #28

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 35 additions & 8 deletions www/Canvas2ImagePlugin.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
cordova.define("org.devgeeks.Canvas2ImagePlugin.Canvas2ImagePlugin", function(require, exports, module) {

//
// Canvas2ImagePlugin.js
// Canvas2ImagePlugin PhoneGap/Cordova plugin
Expand All @@ -7,21 +9,46 @@
// MIT Licensed
//

module.exports = {

saveImageDataToLibrary:function(successCallback, failureCallback, canvasId) {
// successCallback required
module.exports = (function() {

function validateCallBacks(successCallback, failureCallback){
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have to do validation in two apis, so we extract it to this function.


var isValid = true;

if (typeof successCallback != "function") {
console.log("Canvas2ImagePlugin Error: successCallback is not a function");
isValid = false;
}
else if (typeof failureCallback != "function") {
console.log("Canvas2ImagePlugin Error: failureCallback is not a function");
isValid = false;
}
else {
return isValid;
}

function saveImageDataToLibrary(successCallback, failureCallback, canvasId) {
// successCallback required
if (validateCallBacks(successCallback, failureCallback)) {
var canvas = (typeof canvasId === "string") ? document.getElementById(canvasId) : canvasId;
var imageData = canvas.toDataURL().replace(/data:image\/png;base64,/,'');
return cordova.exec(successCallback, failureCallback, "Canvas2ImagePlugin","saveImageDataToLibrary",[imageData]);
return saveDataStringAsImageDataToLibrary(successCallback, failureCallback, imageData);
}
}
};


function saveDataStringAsImageDataToLibrary(successCallback, failureCallback, imageData) {
if (validateCallBacks(successCallback, failureCallback)) {
return cordova.exec(successCallback, failureCallback,
"Canvas2ImagePlugin","saveImageDataToLibrary",[imageData]);
}
}

return {
saveImageDataToLibrary: saveImageDataToLibrary,
saveDataStringAsImageDataToLibrary: saveDataStringAsImageDataToLibrary
}

})()

});