Not clear about usage of jsb.saveImageData and how to read that image data to a texture2d

hi,
if I am reading a texture from a url in cc.loader.load, then inside the closure function I want to write the texture to a file for which I need to get the writable path(I guess that is the only place we can save files) and use jsb.saveImageData to save the texture. now this is only my guess…I don’t know how I would go about using jsb.saveImageData and how I would retrieve the information from the file.
someone please help.
Thanks
Note: First of all I would like to know whether I can save an image to a file and retrieve its contents later on, in cocos creator at all?

Hi there,

These might help you.

@StudioAMK
Hi and thanks for the prompt reply both this time and in the past.
However, I don’t understand the example you provided. If possible, could you please help me out with code for saving a texture2d to a file, and then reading the texture from the file. It is that simple. Also, the texture2d is currently not being displayed, so can’t be extracted from the screen. The code you provided is too confusing.
Sorry and thanks in advance!

Sorry for my very late reply.
I’ll try to find some times to give you a working sample.

Meanwhile, try Cocos Creator -> New Project -> Example Collection

There is a working sample code for taking a screenshot, saving the file and reload the image file.

@StudioAMK,@pandamicro,
hi,
i don’t want to be too pushy but could you please help me out with a working example where i can use the following code in native platforms:

cc.loader.load(imageURL, function (err, texture) {
code to save texture to a file.
});

and then, code to read the texture from a file at that path.
and then, assign that texture to a cc.SpriteFrame.

I just want to let you know that I can save a downloaded texture on the simulator just fine. i take the following approach :

jsb.saveImageData(texture._image._data, texture.width, texture.height, fileFullPath);

and it works just fine.

but on android i get the following errors:

07-27 15:00:39.562 23801 23837 E jswrapper: jsb: ERROR: File /Users/sr/project/build/jsb-default/frameworks/cocos2d-x/cocos/scripting/js-bindings/manual/jsb_conversions.cpp: Line: 1067, Function: seval_to_Data

07-27 15:00:39.562 23801 23837 E jswrapper: Convert parameter to Data failed!

07-27 15:00:39.562 23801 23837 E jswrapper: jsb: ERROR: File /Users/sr/project/build/jsb-default/frameworks/cocos2d-x/cocos/scripting/js-bindings/manual/jsb_global.cpp: Line: 995, Function: js_saveImageData

07-27 15:00:39.562 23801 23837 E jswrapper: js_saveImageData : Error processing arguments

07-27 15:00:39.562 23801 23837 E jswrapper: [ERROR] Failed to invoke js_saveImageData, location: /Users/sr/project/build/jsb-default/frameworks/cocos2d-x/cocos/scripting/js-bindings/manual/jsb_global.cpp:1009

i just want to let you know that :

jsb.saveImageData(texture, texture.width, texture.height, fileFullPath);

doesn’t work either on the simulator or on native android.

thank you!

The first argument data is expected to be a TypedArray, you can print the type name before invoke. @rsamrry