2014年9月4日 星期四

Cocos2d-x 获取网络图片缓存并展示

http://www.cnblogs.com/zisou/p/cocos2dxWLTP.html

今天是年前最后一天上班了,最后一天上班,祝大家马上有各种东西,最后一天也给写一点干货,就是获取网络图片;
经过自己简单封装了一下,实现了获取网络图片,按照比例展示出来,实现方法是cocos2dx - http的异步方法,相信有很多地方都会使用
到着这个需求;比如获取玩家的头像。
我就直接贴代码了,只需一个类就可以搞定;
HttpGetImg.h
复制代码
#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 */
复制代码
HttpGetImg.cpp
复制代码
#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)
{
    
}
复制代码

代码非常简洁简单,先通过:
GetHttpImg(CCObject* obj, string imgurl,int number);
获取到texture,然后把texture存起来,在自己要展示的页面去调用:
CreateImg(CCSprite* tagSprite,CCSprite* oldSprite,CCTexture2D* texture,int newTag)
第一个参数是要展示的父级sprite,第二个是默认头像图片(如果没有下载成功网络图片,显示替代图片),第三个是缓存的texture;

效果图:

结束,祝大家新年快乐;
ps:群号(41131516