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