Base64 is a common method of network transmission coding, which can convert images, text and other formats into binary streams. Transcoding and decoding functions with base64 code in COCOS2D:
int Cocos2d::base64encode (const unsigned char * in, unsigned int inlength, char * * out)
int Cocos2d::base64decode (const unsigned char * in, unsigned int inlength, unsigned char * * out)
We use a decoding function to decode the Base64 code after transcoding the server-side scripting language (such as the Base64_encode function in PHP). After decoding, because the picture format information is not specified and cannot be saved directly to the. jpg format file , it can only be used as raw data to the cocos2dImage object. The Image object is then converted to a texture2d texture Object, which allows the mapping of the Sprite object to be displayed .
On "Init" need to initialize your Instancebool helloworld::init () {////////////////////////////////1. Super Init first if (! Layer::init ()) {return false; } Size visiblesize = Director::getinstance ()->getvisiblesize (); VEC2 origin = Director::getinstance ()->getvisibleorigin ();//Getfiledata if not specified, the read root directory is Resource folder ssize_t size = 0; unsigned char* Titlech = fileutils::getinstance ()->getfiledata ("Image.txt", "R", &size); std::string load_str; Load_str = std::string ((const char*) titlech,size); int len = 0;unsigned char *buffer;len = Base64decode ((unsigned char*) lo Ad_str.c_str (), (unsigned int) load_str.length (), &buffer); image* img = new Image (); bool OK = img->initwithimagedata (buffer, Len); texture2d* tex = new texture2d (); Tex->initwithimage (IMG); auto PSprite2 = Sprite::createwithtexture (Tex);p sprite2- >setposition (VISIBLESIZE.WIDTH/2, VISIBLESIZE.HEIGHT/2); This->addchild (pSprite2, 0); return true;}
Cocos2d-x 3.X receive picture base64 transcoding display