How to load image from server?

Hi,

I want to load an image from server and show it like this…

The yellow background is the scene and on that the image is displayed with close button.

What is the best way to do it? Any sample code would be great… :smile:

There are a couple of ways which I would do it:

  1. Use a cocos2d::experimental::ui::WebView to display it remotely. However, your webview will be blank until the image is loaded.

    cocos2d::experimental::ui::WebView *_webView = cocos2d::experimental::ui::WebView::create();
    _webView->setPosition(winSize/2);
    _webView->setContentSize(winSize * 0.5);
    _webView->loadURL(“http://192.168.1.8/grammarnazi4.jpg”);
    addChild(_webView);

  2. Preload the image when the game starts. You will need to use HttpClient. When you handle the response:

    std::vector*buffer = response->getResponseData();

    Image * img = new Image();
    img->initWithImageData(&(buffer->front()), buffer->size());

    std::string writablePath = FileUtils::getInstance()->getWritablePath();
    writablePath.append();
    img->saveToFile(writablePath.c_str());

1 Like

Archive.zip (3.8 KB)
Check out this code. Hope this helps. I did this long time back. Sorry i didn’t format it.
Check out my games on CodeCanyon,
http://codecanyon.net/item/kung-fu-street-fight-boxing/10519279
:smile:

1 Like

Thanks a lot guys…You saved lots of my time… :smiley: