• 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, 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");

            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,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 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)

    {

        

    }

  • 相关阅读:
    spring4-2-bean配置-2-属性注入细节
    内存管理tcmalloc
    并发视频,怎么hold住高并发
    struts总结
    最近想玩的一个方向
    虚拟化技术漫谈
    陈硕的博客
    木铎
    安全编程资源-苹果
    POOL
  • 原文地址:https://www.cnblogs.com/luorende/p/6255766.html
Copyright © 2020-2023  润新知