【Cocos2d-x】可以显示在线图片的CCSprite

【Cocos2d-x】可以显示在线图片的CCSprite,第1张

概述可以显示在线图片的CCSprite,使用HttpClient异步请求图片数据,当请求成功时,保存图片数据到本地,然后更新CCSprite的纹理,下载中时显示默认图片(可以设置默认图片)。 OnlineImageSprite.h #ifndef __ONLINEIMAGESPRITE_H__#define __ONLINEIMAGESPRITE_H__#include "cocos2d.h"


可以显示在线图片的CCSprite,使用httpClIEnt异步请求图片数据,当请求成功时,保存图片数据到本地,然后更新CCSprite的纹理,下载中时显示默认图片(可以设置默认图片)。


OnlineImageSprite.h

#ifndef __ONliNEIMAGESPRITE_H__#define __ONliNEIMAGESPRITE_H__#include "cocos2d.h"USING_NS_CC;#include "cocos-ext.h"USING_NS_CC_EXT;// 可以显示在线图片的CCSpriteclass OnlineImageSprite:public CCSprite{public:	/*		创建一个显示在线图片的Sprite		* defaultimagePath		默认图片路径(当图片加载中时显示)		* url								图片url	*/	static OnlineImageSprite* create(const char* defaultimagePath,const char* url);		bool init(const char* defaultimagePath,const char* url);	voID onDownloadCompleted(CChttpClIEnt *sender,CChttpResponse *response);protected:	// 下载图片	voID downloadImage(const char* url);	// 获取图片路径	std::string getimagePath(const char* url);	// 图片是否存在	bool isExist(const char* url);	// 保存图片	std::string saveImage(const std::string& data);private:	const char* m_url;};#endif

OnlineImageSprite.cpp

#include "OnlineImageSprite.h"#include <string>using namespace std;   string&   replace_all(string&   str,const   string&   old_value,const   string&   new_value)   {   	while(true)   {   		string::size_type   pos(0);   		if(   (pos=str.find(old_value))!=string::npos   )   			str.replace(pos,old_value.length(),new_value);   		else   break;   	}   	return   str;   }   string&   replace_all_distinct(string&   str,const   string&   new_value)   {   	for(string::size_type   pos(0);   pos!=string::npos;   pos+=new_value.length())   {   		if(   (pos=str.find(old_value,pos))!=string::npos   )   			str.replace(pos,new_value);   		else   break;   	}   	return   str;   }   OnlineImageSprite* OnlineImageSprite::create(const char* defaultimagePath,const char* url){	OnlineImageSprite* pSprite = new OnlineImageSprite();	if (pSprite && pSprite->init(defaultimagePath,url))	{		pSprite->autorelease();		return pSprite;	}	CC_SAFE_DELETE(pSprite);	return NulL;}bool OnlineImageSprite::init(const char* defaultimagePath,const char* url){			m_url = url;	CCTexture2D* pTexture = NulL;	// 图片是否存在,如果不存在使用默认纹理并下载图片	if (isExist(url))	{		const char* path = getimagePath(url).c_str();		pTexture = CCTextureCache::sharedTextureCache()->addImage(path);	}else{		pTexture = CCTextureCache::sharedTextureCache()->addImage(defaultimagePath);		// 下载图片		downloadImage(url);	}		// 初始化	if (CCSprite::initWithTexture(pTexture))	{		return true;	}		return false;}//判断图片是否已经存在bool OnlineImageSprite::isExist(const char* url){		std::string path = getimagePath(url);	return CCfileUtils::sharedfileUtils()->isfileExist(path);}//获取图片全路径std::string OnlineImageSprite::getimagePath(const char* url){	std::string urlStr = url;	replace_all(urlStr,"/","");	replace_all(urlStr,"\",":",".","");	return CCfileUtils::sharedfileUtils()->getWritablePath().append("/").append(urlStr);}// 下载图片voID OnlineImageSprite::downloadImage(const char* url){	// 发起http请求,下载图片	CChttpRequest* request = new CChttpRequest();	request->setUrl(url);	request->setRequestType(CChttpRequest::khttpGet);	request->setResponseCallback(this,httpresponse_selector(OnlineImageSprite::onDownloadCompleted));		CChttpClIEnt::getInstance()->send(request);	request->release();	}// 请求回调voID OnlineImageSprite::onDownloadCompleted(CChttpClIEnt *sender,CChttpResponse *response){	if (!response)	{		return;	}	// 返回码	int statusCode = response->getResponseCode();	char statusstring[64] = {};	sprintf(statusstring,"http Status Code: %d",statusCode);		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 data (buffer->begin(),buffer->end());	std::string path =saveImage(data);	//如果保存图片成功,更新纹理	if (path != "")	{		CCTexture2D* pTexture = CCTextureCache::sharedTextureCache()->addImage(path.c_str());		if (pTexture)		{			setTexture(pTexture);		}			}}// 保存图片std::string OnlineImageSprite::saveImage(const std::string& data){		std::string path = this->getimagePath(m_url);		file* file = fopen(path.c_str(),"wb");	if (file)	{		// 1.buff		// 2.每次写的字节数		// 3.写多少次结束		// 4.文件句柄		fwrite(data.c_str(),1,data.length(),file);		fclose(file);		return path;	}	return "";}

使用示例:
//创建OnlineImageSpirte,参数:1.默认图片路径 2.在线图片的urlCCSprite* pSprite = OnlineImageSprite::create("HelloWorld.png","http://cc.cocimg.com/cocos2dx/image/logo.png");//设置位置pSprite->setposition(ccp(visibleSize.wIDth/2 + origin.x,visibleSize.height/2 + origin.y));//添加到Layerthis->addChild(pSprite,0);

项目地址:https://coding.net/u/linchaolong/p/OnlineImageSprite/git

总结

以上是内存溢出为你收集整理的【Cocos2d-x】可以显示在线图片的CCSprite全部内容,希望文章能够帮你解决【Cocos2d-x】可以显示在线图片的CCSprite所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1023541.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存