1) This is the only way you can convert an image into a string locally (with the excpetion of files loaded with FileReader, see below). Optionally you need to use the server to do it.
2) To get a JPEG image you need to use the argument like this:
var datauri = imgCanvas.toDataURL("image/jpeg", 0.5); //0.5 = optional quality
don't use data:...
in the argument as that will be invalid and it will produce the default PNG as you can see in your result. toDataURL()
can only take a type, ie. image/png
, image/jpeg
etc.
3)
External files
If your image was loaded from a different origin (scheme, server ...) or by using local referenced files (file://
, /my/path/
etc.) CORS kicks in and will prevent you from creating a data-uri, that is: the image will be empty (and therefor invisible).
For external servers you can request permission to use the image from a cross-origin by supplying the crossOrigin
property:
<img src="http://extrernalserver/...." crossOrigin="anonymous" />
or by code (img.crossOrigin = 'anonymous';
) before setting the src
.
It's however up to the server to allow or deny the request.
If it still doesn't work you will need to load your image through a proxy (f.ex. a page on your server that can load and the image externally and serve it from your own server).
Or if you have access to the server's configuration you can add special access allow headers (Access-Control-Allow-Origin: *
, see link below for more details).
CORS (Cross-Origin Resource Sharing) is a security mechanism and can't be worked around other than these ways.
Local files
For local files you will need to use FileReader
- this can turn out to be an advantage as FileReader
comes with a handy method: readAsDataURL()
. This allow you to "upload" the image file directly as a data-uri without going by canvas.
See examples here:
https://developer.mozilla.org/en-US/docs/Web/API/FileReader
Unfortunately you can't just pick the files from code - you will need to provide an input element or a drop zone so the user can pick the files (s)he want to store.
Conclusion
If all these steps are fulfilled to the degree that you actually do get an image the problem possibly is in the other end, the string being truncated being too long to store.
You can verify by checking the length before and after storing the string to see if it has been cut:
console.log(imgAsDataURL.length);
... set / get ...
console.log(pic.length);
Other possibilities:
- The image element is not properly defined.
- A bug in the browser
- A bug in the framework
(I think I covered most of the typical pitfalls?)
Update (missed one, sort of.. ;-p)
OP found a specific in the framework which I'll include here for future reference:
In the end, the issue was with $('#TestScreen_mobileimage_1').src =
policy_shot ;
I'm using Appery.io and they don't support .src
.
It's $('#TestScreen_mobileimage_1').attr("src", policy_shot ) ;
A final note: localStorage
is very limited in regards to storing images. Typical storage space is 2.5 - 5 mb. Each char stored takes 2 bytes and storing a data-uri encoded as base-64 is 33% larger than the original - so space will be scarce. Look into Indexed DB, Web SQL or File API for good alternatives.