Cocos2d-x 3.X receive picture base64 transcoding display

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.