Cocos2d-x 2.1.4 里面已经为我们提供了HttpRequest ,HttpClient,CCHttpResponse用于处理网络相关操作
下面简单利用这些封装好的类,来读取网络图片并创建精灵
1.在工程里面导入HttpRequest.h,HttpClient.h
#include "HelloWorldScene.h" #include "SimpleAudioEngine.h" #include "HttpRequest.h" #include "HttpClient.h" USING_NS_CC; USING_NS_CC_EXT; using namespace std; CCScene* HelloWorld::scene() { CCScene *scene = CCScene::create(); HelloWorld *layer = HelloWorld::create(); scene->addChild(layer); return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { if ( !CCLayer::init() ) { return false; } CCMenuItemImage *pCloseItem = CCMenuItemImage::create( "CloseNormal.png","CloseSelected.png",this,menu_selector(HelloWorld::menuCloseCallback) ); pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20,20) ); CCMenu* pMenu = CCMenu::create(pCloseItem,NULL); pMenu->setPosition( CCPointZero ); this->addChild(pMenu,1); // 这是个静态方法,得到一个httpClient实例 CCHttpClient* httpClient = CCHttpClient::getInstance(); // 创建一个CCHttpRequest对象 CCHttpRequest* httpReq =new CCHttpRequest(); // 设置请求类型 httpReq->setRequestType(CCHttpRequest::kHttpGet); // 设置请求Url(可以更具需要从json 或xml,甚至html里解析获得到这个图片url) httpReq->setUrl("http://avatar.csdn.net/A/6/5/1_qqxj2012.jpg"); // 请求完成后回调 httpReq->setResponseCallback(this,callfuncND_selector(HelloWorld::onGetFinished)); // 为请求设置标签,后面可以根据这个标签来获取我们要的数据 httpReq->setTag("PicGet"); // 设置连接超时时间 httpClient->setTimeoutForConnect(30); httpClient->send(httpReq); httpReq->release(); httpReq=NULL; return true; } void HelloWorld::onGetFinished(CCNode* node,CCObject* obj) { CCHttpResponse* response = (CCHttpResponse*)obj; // 判断是否响应成功 if (!response->isSucceed()) { CCLog("Receive Error! %s\n",response->getErrorBuffer()); return ; } const char* tag = response->getHttpRequest()->getTag(); if (0 == strcmp("PicGet",tag)) { vector<char> *data = response->getResponseData(); int data_length = data->size(); string res; for (int i = 0;i<data_length;++i) { res+=(*data)[i]; } res+='\0'; CCLog("%s",res.c_str()); } // 数据转存 unsigned char* pBuffer = NULL; unsigned long bufferSize = 0; vector<char> *buffer = response->getResponseData(); string path = CCFileUtils::sharedFileUtils()->getWritablePath()+"test.png"; pBuffer = CCFileUtils::sharedFileUtils()->getFileData(path.c_str(),"r",&bufferSize); string buff(buffer->begin(),buffer->end()); //保存到本地文件 CCLOG("path: %s",path.c_str()); FILE *fp = fopen(path.c_str(),"wb+"); fwrite(buff.c_str(),1,buffer->size(),fp); fclose(fp); } void HelloWorld::menuCloseCallback(CCObject* pSender) { unsigned char* pBuffer = NULL; unsigned long bufferSize = 0; string path = CCFileUtils::sharedFileUtils()->getWritablePath()+"test.png"; pBuffer = CCFileUtils::sharedFileUtils()->getFileData(path.c_str(),&bufferSize); if (!pBuffer) { return ; } CCImage* img = new CCImage; img->initWithImageData(pBuffer,bufferSize); free(pBuffer); CCTexture2D* texture = new cocos2d::CCTexture2D(); bool isImg = texture->initWithImage(img); img->release(); if (!isImg) { delete texture; return ; } //这里建议使用成员变量来保存精灵,不然有可能导致显示白色块,出现异常! sp =CCSprite::createWithTexture(texture); CCSprite* sprite = CCSprite::createWithTexture(sp->getTexture()); sprite->setPosition(ccp(240,160)); this->addChild(sprite,10); texture->release(); }
IOS运行截图:
Android 运行截图:
最后说明:
1.Android 运行前,记得在AndroidManifest.xml 开启网络,读取SD卡等权限,不然不会显示!
<!-- 访问internet权限 --> <uses-permission android:name="android.permission.INTERNET"/> <!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 往SDCard写入数据权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.REORDER_TASKS"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
2.这里只是简单实现,并没有实现图片异步加载,后续逐渐完善!
FROM:http://www.xuebuyuan.com/1353683.html