cocos2d-x(ios)下载资源可以使用以下两种方式:
第一种使用libcurl下载图片
使用这种方法需要注意的是,我们需要引入libcurl.a这个库,同时配置对应的库目录和头文件目录具体方法是:
1 导入需要的.a静态数据库
静态库的位置是在
2 导入上图所对应的头文件,头文件的路径是cocos2d根目录/external/curl/include/ios/curl
导入方式在curl文件夹上右键加入新的文件,选择对应的文件夹......
3 配置头文件和库的目录
头文件目录:
库目录:
下载图片的代码:
.h文件
//// CurlDemo.h// LSWGameIOS//// Created by lsw on 14-12-16.////#ifndef __LSWGameIOS__CurlDemo__#define __LSWGameIOS__CurlDemo__#include <stdio.h>#include "cocos2d.h"class CurlDemo : public cocos2d::Layer {public: virtual bool init(); static cocos2d::Scene* createScene(); CREATE_FUNC(CurlDemo);private: voID downLoadPic(); static size_t pWriteCallBack(voID *pData,size_t n,size_t nDataSize,file *stream); static int downloadeProgress(voID *clIEnttp,double fDownLoadTotal,double fDownLoaded,double fUpTotal,double fUpLoaded);};#endif /* defined(__LSWGameIOS__CurlDemo__) */
.cpp文件
//// CurlDemo.cpp// LSWGameIOS//// Created by lsw on 14-12-16.////#include "CurlDemo.h"#include "curl.h"USING_NS_CC;bool CurlDemo::init() { if (!Layer::init()) { return false; } CURLcode nResCode; CURL *pCurl = curl_easy_init(); if (pCurl != nullptr) { auto filename = fileUtils::getInstance()->getWritablePath() +"ceshi.jpg"; file *pfile = fopen(filename.c_str(),"wb+"); cclOG("filename = %s",filename.c_str()); curl_easy_setopt(pCurl,CURLOPT_URL,"http://ww1.sinaimg.cn/large/7f32a2c8jw1e8lyw03zpbj20c8d1ynpd.jpg"); if (pfile != nullptr) { curl_easy_setopt(pCurl,CURLOPT_file,pfile); //设置文件指针 } curl_easy_setopt(pCurl,CURLOPT_WRITEFUNCTION,pWriteCallBack); //回调方法 curl_easy_setopt(pCurl,CURLOPT_VERBOSE,true); curl_easy_setopt(pCurl,CURLOPT_TIMEOUT,60); //超时时间 curl_easy_setopt(pCurl,CURLOPT_nopROGRESS,0L); curl_easy_setopt(pCurl,CURLOPT_PROGRESSFUNCTION,downloadeProgress);//下载进度 nResCode = curl_easy_perform(pCurl); curl_easy_cleanup(pCurl); fclose(pfile); if (nResCode == CURLE_OK) { cclOG("download success"); } else { cclOG("code : %d",nResCode); } } return true;}Scene *CurlDemo::createScene() { auto scene = Scene::create(); auto layer = CurlDemo::create(); scene->addChild(layer); return scene;}size_t CurlDemo::pWriteCallBack(voID *pData,file *stream) { size_t nWriten = fwrite(pData,n,nDataSize,(file *)stream); return nWriten;}int CurlDemo::downloadeProgress(voID *clIEnttp,double fUpLoaded) { if (fDownLoaded >= 0 && fDownLoadTotal != 0) { cclOG(">>>>>>>%0.2f%%\n",100 * (fDownLoaded / fDownLoadTotal)); } return 0;}
第二种方式,使用httpRequest中get方式下载图片
这种方式最大优点就是使用简单,使用cocos2d-x自己封装好的类和方法,设置及其简单。
.cpp文件
voID httpRequestDemo::downloadPicture() { httpRequest *request = new httpRequest(); request->setRequestType(httpRequest::Type::GET); request->setTag("downLoad tag 1"); request->setUrl("http://img12.3lian.com/gaoqing02/06/56/13.jpg"); request->setResponseCallback(CC_CALLBACK_2(httpRequestDemo::onDownloadComplete,this)); httpClIEnt::getInstance()->sendImmediate(request); request->release();}voID httpRequestDemo::onDownloadComplete(httpClIEnt *sender,httpResponse *response) { if (!response) { return; } if (!response->isSucceed()) { cclOG("error %s",response->getErrorBuffer()); return; } std::vector<char> *buffData = response->getResponseData(); char *buff = (char *)malloc(buffData->size()); std::copy(buffData->begin(),buffData->end(),buff); auto filename = fileUtils::getInstance()->getWritablePath() +"ceshi.jpg"; file *fp = fopen(filename.c_str(),"wb+"); fwrite(buff,1,buffData->size(),fp); fclose(fp);}
参考文章:
http://blog.csdn.net/yirancpp/article/details/19123815
http://blog.csdn.net/yirancpp/article/details/19122921
总结以上是内存溢出为你收集整理的使用cocos2d-x 3.2下载图片资源小例子全部内容,希望文章能够帮你解决使用cocos2d-x 3.2下载图片资源小例子所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)