Cocos2d-x 3.X received image base64 transcoding display
Base64 is a common network transmission encoding method. It can convert images, texts, and other formats into binary streams. Transcoding and decoding functions with base64 codes 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)
Here we use the decoding function to decode the base64 code after transcoding by the server-side scripting language (such as the base64_encode function in PHP. After decoding, because the image format information is not specified andCannot be saved directly to. jpg files, Can only be used as raw data to cocos2dImageObject usage. Then convert the Image objectTexture2DTexture object to realize the Sprite objectTexture display.
// On "init" you 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 (); // If getFileData is not specified, the read root directory is the 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 *) load_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); pSprite2-> setPosition (visibleSize. width/2, visibleSize. height/2); this-> addChild (pSprite2, 0); return true ;}