#ifndef __HttpGetImg__
#define __HttpGetImg__
#include “cocos2d.h”
#include ” HttpRequest.h”
#include “HttpClient.h”
USING_NS_CC_EXT;
using namespace std;
USING_NS_CC;
class HttpGetImg: public cocos2d::CCLayer
{
public:
HttpGetImg();
~HttpGetImg();
static void GetHttpImg(CCObject* obj, string imgurl,int number);
void onHttpRequestRptImg(cocos2d::extension::CCHttpClient *sender, cocos2d::extension::CCHttpResponse *response);
p>
//Set the new image size according to the original default avatar ratio
static void setImgSize(int oldhead_w,int oldhead_h,CCSprite* sprite);
< p> //Create a picture to overwrite the original picture
static void CreateImg(CCSprite* tagSprite,CCSprite* oldSprite,CCTexture2D* texture,int newTag);
private:
};
#endif /* HttpGetImg */
# include “HttpGetImg.h”
#include “../DataModel/GlobalInfo.h”
#include “../GameConfig.h”
# include “../Commen/PublicDoFunc.h”
HttpGetImg::HttpGetImg(void)
{
} p>
void HttpGetImg::GetHttpImg(CCObject* obj, string imgurl,int number)
{
CCHttpRequest* request = new CCHttpRequest( );
// required fields
request->setUrl(imgurl.c_str());
request->setRequestType(CCHttpRequest::kHttpGet);
request->setResponseCallback(obj, httpresponse_selector(HttpGetImg::onHttpRequestRptImg));
// optional fields
char thisnumber[10] = “”;
sprintf(thisnumber, “%d”,number);
request->setTag(thisnumber);
CCHttpClient::getInstance()->send (request);
request->release();
}
void HttpGetImg::onHttpRequestRptImg(CCHttpClient *sender, CCHttpResponse *response)
{
char c_tag[20]= “”;
sprintf(c_tag, “%s”,response->getHttpRequest()->getTag());
// // CCLog(“%s completed”, response->getHttpRequest()- >getTag());
string str_tag = c_tag;
if (!response)
{
return;
}
} // You can get original request type from: response->request->reqType
if (0 != strlen(response->getHttpRequest()- >getTag()))
{
CCLog(“%s completed”, response->getHttpRequest()->getTag());
}
if (!response->isSucceed())
{
CCLog(“response failed”);< /p>
CCLog(“error buffer: %s”, response->getErrorBuffer());
return;
}
p>
vector
//create image
CCImage* img = new CCImage ;
img->initWithImageData((unsigned char*)buffer->data(),buffer->size());
//create texture< /p>
CCTexture2D* texture = new CCTexture2D();
bool isImg = texture->initWithImage(img);
img->release();
>
//Save the texture
Texture
//I have downloaded the publicDoFunc asynchronous call that I encapsulated The part of the main thread that performs the image change operation can be encapsulated by itself
//SEL_CallFuncN pfnCallback =callfuncN_selector(View_Room::upPlayerHead);
//CCNode* nd = CCNode::create ();
//buffer->clear();
//nd->setTag(CCString::create(str_tag)->intValue());
//PublicDoFunc::toDoFuncN(LAYER_ROOM, pfnCallback,nd);
}
void HttpGetImg::setImgSize(int oldhead_w,int oldhead_h,CCSprite* sprite)
{
float s_X=oldhead_w/sprite->getContentSize().width;
float s_Y=oldhead_h/sprite-> getContentSize().height;
sprite->setScaleX(s_X);
sprite->setScaleY(s_Y);
}
void HttpGetImg::CreateImg(CCSprite* tagSprite,CCSprite* oldSprite,CCTexture2D* texture,int newTag)
{
int ol dw =oldSprite->getContentSize().width;
int oldh =oldSprite->getContentSize().height;
if(texture!=NULL)
{
CCSprite* sprite = CCSprite::createWithTexture(texture);
setImgSize(oldw,oldh,sprite);
sprite->setPosition(oldSprite->getPosition());
tagSprite->addChild(sprite,1,newTag);
texture->release ();
}
}
HttpGetImg::~HttpGetImg(void) p>
{
}