I have try these but not success. It is not working on native and website.
cc.Sprite.create("data:image/png;base64,iVBORw0KGgoAAAANSU…");
cc.Sprite.create("iVBORw0KGgoAAAANSU…");
new cc.Sprite("data:image/png;base64,iVBORw0KGgoAAAANSU…");
new cc.Sprite("iVBORw0KGgoAAAANSU…");
Then i try cc.Texture2D() but also failure.
var texture = new cc.Texture2D();
texture.initWithData(base64, cc.Texture2D.PIXEL_FORMAT_RGB8888, 100, 100, cc.size(100, 100));
I can create image with the base64 in website with
new cc.newElement("IMG");
But it is hard to control the DOM element visibility so i would like to create the image with a cc.Sprite.
I have tried your solution.
There is no error in the log but the cc.Sprite is not showing any image.
But i found a solution which is working on website.
var imgElement = new cc.newElement("IMG");
imgElement.setAttribute("src", base64Data);
cc.textureCache.cacheImage("uniqueKey", imgElement);
var sprite = new cc.Sprite("uniqueKey");
You need to generate an “uniqueKey” as path for the cache.
Do you mean you want to create a sprite from base64 image?
This code is working to create sprite in web
var imgElement = new cc.newElement("IMG");
imgElement.setAttribute("src", base64Data);
cc.textureCache.cacheImage("uniqueKey", imgElement);
var sprite = new cc.Sprite("uniqueKey");
However, it is a bit tricky for native.
You need to do it in C++ for JSB, something like this.
Image *image = new Image();
image->initWithImageData((unsigned char*)bufferPtr, lengthOfArray);
Texture2D* texture = new Texture2D();
texture->initWithImage(image);
Sprite* sprite = new Sprite();
sprite->initWithTexture(texture);
In JSB:
1.Create cc.Image with base64 data (transformed to uint8array and then pass to JSB and transform to a unsigned char * data), need manual bindings
2.Create a texture with the cc.Image object
3.Create a sprite
It is a little complicated to do in native.
1.Basically, you have to create a new function which accept your base64 data in C++.
My code is:
var url = “data:image/jpeg;base64,dsfsdsdvsdv…”
var texture2d = new cc.Texture2D();
var img = cc.loader.getRes(url);
texture2d.initWithElement(img);
texture2d.handleLoadedTexture();
var logo = cc.Sprite.create(texture2d);
app.addChild(logo);
when i run: it notify that: “type is null”
what is my error?
how can i fix it.
The code you using is for web, won’t work in native.
I only done create cc.Sprite with base64 in web, here is the example code
var sprite = new cc.Sprite();
sprite.setPositon(cc.winSize.width/2, cc.winSize.height/2);
this.addChild(sprite);
cc.DOM.convert(sprite);
var spriteDom = new cc.newElement("IMG");
sprite.dom.appendChild(spriteDom);
spriteDom.setAttribute("src", base64Data);
var texture2d = new cc.Texture2D();
texture2d.initWithElement(spriteDom);
texture2d.handleLoadedTexture();
var newSprite = new cc.Sprite(texture2d);
newSprite.setPositon(cc.winSize.width/2, cc.winSize.height/2);
this.addChild(newSprite);
var aUint8 = new Uint8Array(4);
aUint8[0] = 104;
aUint8[1] = 111;
aUint8[2] = 108;
aUint8[3] = 97;
cc.Sprite.createWithBase64(aUint8);
And it gets to the function, cause when the argument was an int and I passed an uint8 from javascript it arrived well and I printed it via log, but this give me this error:
Finally I have found a solution usign the genbindings.py and leaving (in both the sdk and the frameworks/js-bindings/etcetera alothough i think it’s only needed it in the later) CCSprite like this:
CCSprite.cpp:
Sprite* Sprite::createWithBase64(const std::string& filename)
{
int len = 0;
unsigned char *buffer;
len = base64Decode((unsigned char*)filename.c_str(), (unsigned int)filename.length(), &buffer);
Image *img = new Image();
bool ok = img->initWithImageData(buffer,len);
if(!ok) {
dbgprint("ERROR CREATING IMAGE WITH BASE64");
return nullptr;
}
Texture2D* texture = new Texture2D();
texture->initWithImage(img);
Sprite* sprite = new Sprite();
if(sprite && sprite->initWithTexture(texture)){
sprite->autorelease();
return sprite;
}
CC_SAFE_DELETE(sprite);
return nullptr;
}