Skip to content

Instantly share code, notes, and snippets.

@gin0606
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save gin0606/b1ef3cfce83439e5e95f to your computer and use it in GitHub Desktop.

Select an option

Save gin0606/b1ef3cfce83439e5e95f to your computer and use it in GitHub Desktop.
cocos2d-xでダウンロードした画像をファイルに保存せず表示する ref: http://qiita.com/gin0606/items/b498b016acbb212dc6f3
auto url = "任意の画像のURL";
auto request = new cocos2d::network::HttpRequest();
request->setUrl(url);
request->setRequestType(cocos2d::network::HttpRequest::Type::GET);
request->setResponseCallback([this, url](cocos2d::network::HttpClient *, cocos2d::network::HttpResponse *response) {
if (!response->isSucceed()) {return;}
auto responseData = response->getResponseData();
auto bytes = reinterpret_cast<unsigned char *>(&(responseData->front()));
auto data = cocos2d::Data();
data.copy(&(bytes.front()), bytes.size());
auto image = new (std::nothrow) cocos2d::Image();
if (!image) {return;}
image->initWithImageData(data.getBytes(), data.getSize());
auto texture = cocos2d::Director::getInstance()->getTextureCache()->addImage(image, url);
auto spriteFrame = cocos2d::SpriteFrame::createWithTexture(texture, cocos2d::Rect(0, 0, texture->getContentSize().width, texture->getContentSize().height));
cocos2d::SpriteFrameCache::getInstance()->addSpriteFrame(spriteFrame, url);
CC_SAFE_RELEASE(image);
auto sprite = cocos2d::Sprite::createWithTexture(texture);
this->addChild(sprite);
// or
auto imageView = cocos2d::ui::ImageView::create(url, cocos2d::ui::TextureResType::PLIST);
this->addChild(imageView);
});
cocos2d::network::HttpClient::getInstance()->send(request);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment