Cocos2d-x 3.X received image base64 transcoding display

Source: Internet
Author: User

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 ;}

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.