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
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);
}
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.
@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.
@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
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.