downloading using httpclient

downloading using httpclient
0.0 0



I am following the instructions in
I want to download image from the url. For that how do I use the HttpResponse~~>getResponseData?
Because HttpResponse~~>getResponseData() returns a vector, how can I read it into a ccImage object ?

Thanks for any help…


Try using CCImage method initWithImageData.


Hi Leszek,

Thanks for your reply.

But I have already tried all that.

Here is what I have done :
in onHttpRequestCompleted function,

cocos2d::extension::CCHttpResponse **response = data;
std::vector**buffer = response~~>getResponseData;
CCImage* img ;
img~~>initWithImageData(buffer, sizeof(buffer));

cocos2d::CCTexture2D* texture ;
texture~~>initWithImage; //in this line I am getting exception
CCSprite* sprite = CCSprite::createWithTexture;
this~~>addChild(sprite, 10);

i have also tried like :
img->initWithImageData(buffer, 2841,CCImage::kFmtPng,160,160,8); but still not getting.

Please see if you can correct this.



You are trying to use a pointer which does not point to any object:

CCImage* img ;
img->initWithImageData(buffer, sizeof(buffer));

And you are doing the same with CCTexture2D. First create CCImage and CCTexture2D with new operator like CCImage* img = new CCImage(); and then you can init it with initWithImageData. And remember that when you new a CCObject then you need to release it or set autorelease to prevent memory leak.


Ya, you are right.I tried that ,I mean creating CCImage and CCTexture2D with new operator.

But img->initWithImageData(buffer, 2841,CCImage::kFmtPng,160,160,8) returns false.


So did you debug it? Why it is returning false?


I didnt get any clue when i debugged :frowning: .
Are you sure parameters of the function initWithImageData() are correct ?


Oh I see that response~~>getResponseData returns a pointer to vector so you should pass it to init like this: img~~>initWithImageData(&(buffer~~>front), buffer~~>size());


Wow …that worked

Thank you Thank You….thanks alot :slight_smile: