cocos2d-x CCHttpRequest获取网络图片并显示
转自:http://www.cnblogs.com/hzj730/p/3178431.html
//图片结构
class imgstruct : public CCObject
{
public:
imgstruct(const char* iName, const char* idStr, CCLayer* _layer, bool mask)
{
imageName = string(iName);
observerId = string(idStr);
layer = _layer;
useMask = mask;
}
~imgstruct()
{
} string imageName;
string observerId;
CCLayer* layer; //父layer
bool useMask;
}; //图片监听,下载完成触发
class CCImageNotificationCenter : public CCNotificationCenter
{
public:
DECLARE_SINGLETON_CLASS(CCImageNotificationCenter);
CCImageNotificationCenter();
~CCImageNotificationCenter(); CCString addObserver(const char *imageName,CCLayer* layer,bool useMask);
void imageLoaded();
void removeObserver(const char *name); void postNotification(const char *name, CCObject *object); void imageLoaded(CCObject *obj);
static CCSprite* getSpriteFromWriteablePath(const char* name); private:
CCNotificationCenter m_notificationCenter;
int m_observerID;
};
具体实现
CCString CCImageNotificationCenter::addObserver(const char *imageName, CCLayer* layer, bool useMask)
{
CCString* observerIDstr = CCString::createWithFormat("%d",m_observerID); m_notificationCenter.addObserver(this, callfuncO_selector(CCImageNotificationCenter::imageLoaded), observerIDstr->getCString(), new imgstruct(imageName, observerIDstr->getCString(), layer, useMask)); m_observerID++;
return observerIDstr->getCString();
} void CCImageNotificationCenter::removeObserver(const char *name)
{
m_notificationCenter.removeObserver(this, name);
} void CCImageNotificationCenter::postNotification(const char *name, CCObject *object)
{
m_notificationCenter.postNotification(name, object);
} void CCImageNotificationCenter::imageLoaded(CCObject *obj)
{
imgstruct* img = (imgstruct*)obj;
CCLOG("imageLoaded success,imageName:%s",img->imageName.c_str());
CCSprite* sprite = CCImageNotificationCenter::getSpriteFromWriteablePath(img->imageName.c_str());
CCLOG("got sprite 0x%X", sprite);
if (img->useMask)
{
img->layer->addChild(CCImageNotificationCenter::createMaskedSprite(sprite,"mask.png"));
}
else
{
float scale_ = (float) img->layer->getContentSize().width / (float)sprite->getContentSize().width;
sprite->setAnchorPoint(ccp(,));
sprite->setScale( scale_ );
img->layer->addChild(sprite);
}
this->removeObserver(img->observerId.c_str());
img->release();
} CCSprite* CCImageNotificationCenter::getSpriteFromWriteablePath(const char* name)
{
std::string path = CCFileUtils::sharedFileUtils()->getWriteablePath();
path += name;
FILE* fp = fopen(path.c_str(),"rb");
if (!fp)
{
return NULL;
}
fseek(fp,,SEEK_END);
int len = ftell(fp);
fseek(fp,,SEEK_SET);
char* buf = (char*)malloc(len);
fread(buf,len,,fp);
fclose(fp);
CCImage* img = new CCImage;
img->initWithImageData(buf,len);
free(buf);
cocos2d::CCTexture2D* texture = new cocos2d::CCTexture2D();
bool isImg = texture->initWithImage(img);
img->release();
if (!isImg)
{
delete texture;
return CCSprite::create("default.png");//加载资源并非图片时返回的默认图
}
CCSprite* sprite = CCSprite::createWithTexture(texture);
texture->release();
return sprite;
}
实现ImageDownload类
class ImageDownloader : public cocos2d::CCObject
{
DECLARE_SINGLETON_CLASS(ImageDownloader);
public:
ImageDownloader();
~ImageDownloader(); void CreateLuaFunc(); void SendHttpRequest(const char* url, CCLayer* layer, const char* filename);
void HttpRequestComplete(CCHttpClient *sender, CCHttpResponse *response); CCString observerID;
CCLayer* container;
bool useMask;
};
void ImageDownloader::SendHttpRequest(const char* url, CCLayer* layer, const char* filename)
{
CCHttpRequest* request = new CCHttpRequest();
this->container = layer;
if (this->container)
{
CCImageNotificationCenter::CreateInstance();
this->observerID = CCImageNotificationCenter::GetInstance()->addObserver(filename,container,useMask);
}
request->setUrl(url);
// request->setUrl("http://neoimaging.beareyes.com.cn/png2/ni_png_2_1518.png");
request->setRequestType(CCHttpRequest::kHttpGet);
request->setResponseCallback(this, httpresponse_selector(ImageDownloader::HttpRequestComplete));
request->setTag("GET IMAGE");
CCHttpClient::getInstance()->send(request);
request->release();
} void ImageDownloader::HttpRequestComplete(CCHttpClient *sender, CCHttpResponse *response)
{
if (!response)
{
return;
} // You can get original request type from: response->request->reqType
if ( != strlen(response->getHttpRequest()->getTag()))
{
CCLog("%s completed", response->getHttpRequest()->getTag());
} int statusCode = response->getResponseCode();
char statusString[] = {};
sprintf(statusString, "HTTP Status Code: %d, tag = %s", statusCode, response->getHttpRequest()->getTag());
CCLog("response code: %d", statusCode); if (!response->isSucceed())
{
CCLog("response failed");
CCLog("error buffer: %s", response->getErrorBuffer());
return;
} // dump data
std::vector<char> *buffer = response->getResponseData();
std::string path = CCFileUtils::sharedFileUtils()->getWriteablePath();
std::string bufffff(buffer->begin(),buffer->end()); //保存到本地文件
path += "dl.png";
CCLOG("path: %s",path.c_str());
FILE *fp = fopen(path.c_str(), "wb+");
fwrite(bufffff.c_str(), ,buffer->size(), fp);
fclose(fp); //传入container的下载请求会添加侦听,待下载完毕自动添加到container上
if (this->container)
{
// container 是一个CCLayer ,用来显示动态加载的资源
CCImageNotificationCenter::GetInstance()->cocos2d::CCNotificationCenter::postNotification(this->observerID.getCString());
}
}
cocos2d-x CCHttpRequest获取网络图片并显示的更多相关文章
- Cocos2d-x利用CCHttpRequest获取网络图片并显示
利用CCHttpRequest获取网上http地址的图片并缓存到本地生成CCSprite用于显示 //图片结构class imgstruct : public CCObject { public: i ...
- 获取网络图片并显示在picturbox上,byte[]数组转换成Image:
private void getWebPicture_Click(object sender, EventArgs e) { WebRequest request = WebRequest.Creat ...
- 【cocos2d-x 手游研发小技巧(5)获取网络图片缓存并展示】
今天是年前最后一天上班了,最后一天上班,祝大家马上有各种东西,最后一天也给写一点干货,就是获取网络图片: 经过自己简单封装了一下,实现了获取网络图片,按照比例展示出来,实现方法是cocos2dx - ...
- cocos2d-x C++ 获取网络图片缓存并展示
#ifndef __HttpGetImg__ #define __HttpGetImg__ #include "cocos2d.h" #include "HttpRequ ...
- 分享一个安卓中异步获取网络图片并自适应大小的第三方程序(来自github)
安卓中获取网络图片,生成缓存 用安卓手机,因为手机流量的限制,所以我们在做应用时,要尽量为用户考虑,尽量少耗点用户的流量,而在应用中网络图片的显示无疑是消耗流量最大的,所以我们可以采取压缩图片或者将图 ...
- 根据url路径获取图片并显示到ListView中
项目开发中我们需要从网络获取图片显示到控件中,很多开源框架如Picasso可以实现图片下载和缓存功能.这里介绍的是一种简易的网络图片获取方式并把它显示到ListView中. 本案例实现的效果如下: 项 ...
- tableView 获取网络图片,并且设置为圆角(优化,fps)
代码地址如下:http://www.demodashi.com/demo/11088.html 一.准备工作 例子比较精简,没有什么特殊要求,具备Xocde8.0左右版本的就好 二.程序实现 1.相关 ...
- URL转Drawable之 Android中获取网络图片的三种方法
转载自: http://doinone.iteye.com/blog/1074283 Android中获取网络图片是一件耗时的操作,如果直接获取有可能会出现应用程序无响应(ANR:Applicatio ...
- js获取当前时间显示在页面上
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
随机推荐
- 规范-Git打标签与版本控制
Git打标签与版本控制规范 前言 本文适用于使用Git做VCS(版本控制系统)的场景. 用过Git的程序猿,都喜欢其分布式架构带来的commit快感.不用像使用SVN这种集中式版本管理系统,每一次提交 ...
- PS CC 破解安装教程(亲测可用)
PS CC版本新增了一些更高效的切图工具,比如可以直接右击图层转化为PNG图像 下面介绍一种亲测可用的破解安装教程 软件下载地址:https://pan.baidu.com/s/1dFJFqhj 一. ...
- WPF的ListView控件自定义布局用法实例
正文: 如何布局是在App.xaml中定义源码如下 <Application x:Class="CWebsSynAssistant.App" xmlns="ht ...
- iOS7 文本转语音 AVSpeechSynthesizer -转载-
http://www.cnblogs.com/qingjoin/p/3160945.html iOS7 的这个功能确实不错.我刚试了下,用官方提供的API ,简单的几句代码就能实现文本转语音! Xco ...
- cocos2d-x 3.2 for wp8-xaml应用商店提交应用时出现的API错误(不能用CreateEventExA)解决的方法
好不easy做完一个游戏.提交到商店显示"本地API不支持CreateEventExA"之类的错误提示 于是我在整个解决方式里查找CreateEventExA,发现没有,却在Aud ...
- 九度OJ 1057:众数 (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:8431 解决:2819 题目描述: 输入20个数,每个数都在1-10之间,求1-10中的众数(众数就是出现次数最多的数,如果存在一样多次数的 ...
- HTML 学习笔记 JQuery(事件)
加载DOM 以浏览器加载文档为例,在页面加载完毕后,浏览器会通过JavaScript为DOM元素添加事件.在常规的JavaScript中,通常使用window.onload方法,在JQuery中通常使 ...
- 前端面试题(一)JS篇
内置类型 JS 中分为七种内置类型,七种内置类型又分为两大类型:基本类型和对象(Object). 基本类型有六种: null,undefined,boolean,number,string,symbo ...
- nvl()与regexp_replace()
NVL(字段,0):将空值转化为0 regexp_replace(字段, ‘[1-9]‘ ,'') is not null; 将数字转化为0
- 百度小程序转换微信小程序
Python脚本,一键转换Github地址:https://github.com/DWmelon/py-transfer-BdToWx 运行条件 具备Python环境,可在命令行中使用Python命令 ...