Skip to content

Instantly share code, notes, and snippets.

@ysugimoto ysugimoto/getImage.cpp
Last active Jun 1, 2017

Embed
What would you like to do?
Exxternal image shows in Sprite on cocos2d-x v3.x
#include "network/HttpRequest.h"
#include "network/HttpClient.h"
#include "network/HttpResponse.h"
using namespace cocos2d::network;
HttpRequest* request = new HttpRequest();
request->setUrl("http://example.com/external/image.png");
request->setRequestType(HttpRequest::Type::GET);
request->setResponseCallback([this](HttpClient *sender, HttpResponse *response) {
if ( ! response->isSucceed() ) {
return;
}
std::vector<char> *buffer = response->getResponseData();
Image* image = new Image();
image->initWithImageData(reinterpret_cast<unsigned char*>(&(buffer->front())), buffer->size());
Texture2D* texture = new Texture2D();
texture->initWithImage(image);
Sprite* sprite = Sprite::createWithTexture(texture);
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
Size size = sprite->getContentSize();
sprite->setPosition(Vec2((visibleSize.width + origin.x)/2,
(visibleSize.height + origin.y)/2));
this->addChild(sprite, 100);
image->release();
texture->release();
});
HttpClient::getInstance()->send(request);
request->release();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.