cocos2dx 獲取網路圖片並建立精靈顯示到介面上
阿新 • • 發佈:2019-02-06
參考文章:http://blog.csdn.net/candyforever/article/details/10185855
TestGetPicFromHttp.h
#ifndef __TESTGETPICFROMHTTP_H__ #define __TESTGETPICFROMHTTP_H__ #include "cocos2d.h" #include "cocos-ext.h" USING_NS_CC; USING_NS_CC_EXT; class TestGetPicFromHttp : public cocos2d::CCLayer { private: TestGetPicFromHttp(); ~TestGetPicFromHttp(); public: virtual bool init(); CREATE_FUNC(TestGetPicFromHttp); void initHttpInfo(void); void onGetFinished(CCHttpClient *client, CCHttpResponse *response); void setGetTextureVisible(void); static TestGetPicFromHttp* get_instance(void); private: static TestGetPicFromHttp* m_instance; }; #endif // __TESTGETPICFROMHTTP_H__
TestGetPicFromHttp.cpp
#include "TestGetPicFromHttp.h" #include <vector> #include <string> using namespace std; TestGetPicFromHttp* TestGetPicFromHttp::m_instance = 0; TestGetPicFromHttp::TestGetPicFromHttp() { } TestGetPicFromHttp::~TestGetPicFromHttp() { } // on "init" you need to initialize your instance bool TestGetPicFromHttp::init() { if ( !CCLayer::init() ) { return false; } initHttpInfo(); return true; } void TestGetPicFromHttp::initHttpInfo(void) { CCHttpClient* httpClient = CCHttpClient::getInstance(); CCHttpRequest* httpRequest = new CCHttpRequest(); httpRequest->setRequestType(CCHttpRequest::kHttpGet); httpRequest->setUrl("http://img.my.csdn.net/uploads/201405/12/1399882123_6429.jpg"); httpRequest->setResponseCallback(this, httpresponse_selector(TestGetPicFromHttp::onGetFinished)); httpRequest->setTag("PicGet"); httpClient->setTimeoutForConnect(30); //設定連結超時時間 httpClient->send(httpRequest); httpRequest->release(); httpRequest = NULL; } void TestGetPicFromHttp::onGetFinished(CCHttpClient *client, CCHttpResponse *response) { // 判斷是否響應成功 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()+"head2.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); setGetTextureVisible();//儲存完畢後 在呼叫顯示貼圖函式 } void TestGetPicFromHttp::setGetTextureVisible(void) { unsigned char* pBuffer = NULL; unsigned long bufferSize = 0; string path = CCFileUtils::sharedFileUtils()->getWritablePath()+"head2.png"; pBuffer = CCFileUtils::sharedFileUtils()->getFileData(path.c_str(), "r", &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 ; } //這裡建議使用成員變數來儲存精靈,不然有可能導致顯示白色塊,出現異常! CCSprite* m_get_sprite = CCSprite::createWithTexture(texture); CCSprite* sprite = CCSprite::createWithTexture(m_get_sprite->getTexture()); if (sprite) { this->addChild(sprite); CCSize winSize = CCDirector::sharedDirector()->getWinSize(); this->setPosition(ccp(winSize.width/2, winSize.height/2)); } texture->release(); } TestGetPicFromHttp* TestGetPicFromHttp::get_instance(void) { if (m_instance == NULL) { m_instance = TestGetPicFromHttp::create(); } return m_instance; }
使用方式:在HelloWorldScene.cpp 中呼叫
TestGetPicFromHttp* httpLayer = TestGetPicFromHttp::get_instance();
this->addChild(httpLayer);
httpLayer->init();
最後說明:
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>