[Solved] making sprite from a URL in cocos2d-x IOS game in xcode

I have a URL https://dl.dropboxusercontent.com/u/37517735/achievementtest.html
I want to make a CCSprite from this

CCSprite *c=CCSprite::create(URL);
this->addchild(c);

Is there a way to do this
I have already visited http://www.cocos2d-x.org/forums/6/topics/12507
But here I am unable to find curl.There is no keyword like curl
Can anyone provide me with some logic on how to do this.I ll be thankful

It won’t work like this.

You have to Http request the image url and once you got the response, you can create image from that response data and save it to your phone. After that you can create the sprite from the save image.

void ClassName::downLoadImage()
{
        std::string strImage = "img.png";

        CCHttpRequest* request = new CCHttpRequest();
        request->setUrl("url of your image");
        request->setRequestType(CCHttpRequest::kHttpGet);
        request->setResponseCallback(this, httpresponse_selector(ClassName::onImageDownLoaded));
        request->setTag(strImage.c_str());
        CCHttpClient::getInstance()->send(request);
        request->release();
}

void ClassName::onImageDownLoaded(CCHttpClient* pSender, CCHttpResponse* pResponse)
{
    CCHttpResponse* response = pResponse;
    
    if (!response)
    {
        CCLog("No Response");
        return;
    }
    int statusCode = response->getResponseCode();
    
    char statusString[64] = {};
    sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag());
    CCLog("response code: %d", statusCode);
    
    if (!response->isSucceed())
    {
        CCLog("response failed");
        CCLog("error buffer: %s", response->getErrorBuffer());
        return;
    }
    std::vector<char> *buffer = response->getResponseData();
 
    CCImage * img=new CCImage();
    img->initWithImageData(&(buffer->front()), buffer->size());
    
    // Save image file to device.
    std::string writablePath = CCFileUtils::sharedFileUtils()->getWritablePath();
    writablePath.append(response->getHttpRequest()->getTag());
    

     //Now create Sprite from downloaded image
     CCSprite* pSprite = CCSprite::create(writablePath.c_str());
     addchild(pSprite);
}

@lazydevx

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

This line is not working showing error as use of class template std::vector require template arguments.Then we change this line as

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

Then it was ok.But after the code running,it shows response as null and therefore sprite also null and error comes as child assertion failed
My url that i mentioned is fine and image is not saving at the writable simulator path.

Sorry my mistake

it would be like that only
std::vector*buffer = response->getResponseData();

@lazydevx
no no…that is ok…But after the code running,it shows response as null and therefore sprite also null and error comes as child assertion failed
My url that i mentioned is fine and image is not saving at the writable simulator path.

use this link https://dl.dropboxusercontent.com/u/37517735/icon_test.jpg
image name and its extension should be included in url.

@lazydevx still not working…response is coming null. So imp.png is not made in the iphone simulator path.therefore sigabrt error for sprite. Do u have a running sample?If so please share. I will be very thankful
I am also attaching my screen shot

Update the code

// Save image file to device.
std::string writablePath = CCFileUtils::sharedFileUtils()->getWritablePath();
writablePath.append(response->getHttpRequest()->getTag());

// add this line
img->saveToFile(writablePath.c_str());

The code seems to be inspired from EziSocial Library.
https://github.com/ezibyte/EziSocial-Plugin/blob/master/For_Cocos2dx_v2.2/ezibyte/ezisocial/src/EziSocialObject.cpp#L1051

Anyway that’s not the point of concern. I am updating EziSocial library for Cocos2dx 3.x and it seems images are not downloading properly with Cocos2dx 3.x version. The sample dropbox images looks like the attached image. Any idea?

Im running into the same problem as well. Im using the Cocos2d-x version 3.0.
The file that was downloaded using HttpClient was not working, as it failed the Image class Parser.
I tried using the native Android codes; URL, InputStream and OutputStream on Java.
It works fine with the same URL path and save the file to the disk.
Its seems to have some issues with either the HttpClient or the parser.

I have created a ticket,
http://www.cocos2d-x.org/issues/5197

Lets hopes they will response soon.

I would like the answer it in forum. And sorry for late reply.

@jake22599
Could you please paste some pseudocode?
Thanks.

@zhangxm I have done simple debugging and found that the request has a hard limit on the data for some reason.

I mean, if the response is too long, not all of it will be received. Here is my test:

network::HttpClient *client = network::HttpClient::getInstance();
network::HttpRequest *request = new network::HttpRequest();

request->setUrl("http://mazyod.com/");
request->setRequestType(network::HttpRequest::Type::GET);
request->setResponseCallback([=](network::HttpClient* client, network::HttpResponse* response)
{
    auto v = response->getResponseData();
    char *buffer = new char[v->size()];
    std::copy(v->begin(), v->end(), buffer);

    CCLOG("DONE! %s - %ld - %s", response->isSucceed() ? "YES" : "NO", response->getResponseCode(), buffer);
});
client->send(request);
request->release();

I get (Sorry, HTML not showing because of a bug in the forums):

cocos2d: DONE! YES - 200 - 
<!DOCTYPE html>
<!--[if IEMobile 7 ]><html class="no-js iem7"><![endif]-->
<!--[if lt IE 9]><html class="no-js lte-ie8"><![endif]-->
<!--[if (gt IE 8)|(gt IEMobile 7)|!(IEMobile)|!(IE)]><!--><html class="no-js" lang="en"><!--<![endif]-->
<head> 
...
what&rsquo;s around you, &hellip; is ba

I took away the middle part of the response, but you see in the end, it doesn’t doesn’t download the whole thing.

@Mazyod
Thanks for the information. #5221 is created for this issue.