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 3 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
36 changes: 29 additions & 7 deletions www/Canvas2ImagePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,43 @@
// 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) {
Copy link
Author

Choose a reason for hiding this comment

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

Using this function to save data string as image to library.
Ex:

var xhr = new XMLHttpRequest();
xhr.responseType="blob";
xhr.open("get", "http://www.mysite.com.tw/foo.jpg");
xhr.send();
var data = xhr.response.split(",")[1];
canvas2ImagePlugin.saveDataStringAsImageDataToLibrary(function(){console.log(true)}, function(){console.log(false)}, data);

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

return {
saveImageDataToLibrary: saveImageDataToLibrary,
saveDataStringAsImageDataToLibrary: saveDataStringAsImageDataToLibrary
}

})();