I try to convert a canvas image into a blob. This can be done with the toBlob() polyfile. It works on desktop browsers but on iPhone I do not get any blob. The size is always zero.
Here is a JsFiddle for this: http://jsfiddle.net/confile/h7zV3/
How do I get the Blob from the canvas on the iPhone?
Here is the code I used:
<input id="file" type="file" />
<img id="img">
<br>canvas<br>
<canvas id="mycanvas" ></canvas>
<script type="text/javascript">
$(function(){
var $inputFile = $("#file"),
inputFile = $inputFile[0],
$img = $("#img"),
img = $img[0];
var tmpImage = img; // document.createElement("img");
$inputFile.on("change", function() {
var files = inputFile.files;
if (!files || !files.length)
return
var reader = new FileReader()
reader.onload = function(progressEvent) {
console.log("reader.result: "+reader.result);
tmpImage.onload = function() {
var canvas = $("#mycanvas")[0]; //document.createElement("canvas"),
context = canvas.getContext("2d");
canvas.width = this.width;
canvas.height = this.height;
context.drawImage(this, 0, 0);
var dataUrlValue = canvas.toDataURL(); //canvas.toDataURL("image/jpeg");
alert(dataUrlValue);
var myBlob1 = dataURItoBlob(dataUrlValue);
console.log(myBlob1);
alert(myBlob1.size);
canvas.toBlob(function(blob) {
console.log("done");
alert(blob.size);
}, 'image/jpeg');
}; // end onload
tmpImage.src = reader.result;
};
reader.readAsDataURL(files[0]);
});
}); // end JQuery
</script>
Use this plugin JavaScript-Canvas-to-Blob to convert canvas elements into Blob objects.
Then use the canvas.toBlob() method in the same way as the native implementation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With