码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2018-06-04 16:56:34      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:head   null   ccsprite   you   his   number   back   sprintf   nal   

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

{

    

}

 

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

标签:head   null   ccsprite   you   his   number   back   sprintf   nal   

原文地址:https://www.cnblogs.com/luorende/p/6255766.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!