cocos2d-x C++ 获取网络图片缓存并展示

前端之家收集整理的这篇文章主要介绍了cocos2d-x C++ 获取网络图片缓存并展示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

#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);

 

    //按原来默认头像比例设置新图片大小

    static void setImgSize(int oldhead_w,int oldhead_h,CCSprite* sprite);

    

    //创建图片覆盖原有图片

    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)

{

    

}

 

void HttpGetImg::GetHttpImg(CCObject* obj,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");

        CCLog("error buffer: %s",response->getErrorBuffer());

        return;

    }

    

    vector<char> *buffer = response->getResponseData();

    

    //create image

    CCImage* img = new CCImage;

    img->initWithImageData((unsigned char*)buffer->data(),buffer->size());

    

    //create texture

    CCTexture2D* texture = new CCTexture2D();

    bool isImg = texture->initWithImage(img);

    img->release();

    

   //将texture存下来

    texture

 

    //已下是自己封装的PublicDoFunc异步调用主线程执行换图操作的部分,可以自己去封装

    //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,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,int newTag)

{

    int oldw =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)

{

    

}

猜你在找的Cocos2d-x相关文章