首页 技术 正文
技术 2022年11月7日
0 收藏 871 点赞 700 浏览 3323 个字

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, size_t n, size_t nDataSize, FILE *stream) {
size_t nWriten = fwrite(pData, n, nDataSize, (FILE *)stream);
return nWriten;
}int CurlDemo::downloadeProgress(void *clienttp, double fDownLoadTotal, double fDownLoaded, double fUpTotal, 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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,941
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,465
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,280
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,094
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,727
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,765