---测试 http请求网络图片 的代码
local function httpRequestCompleted(statusCode,tagNum,image)
print("图片数据请求结果 statusCode:"..statusCode.." tag:"..tagNum)
---200表示获取网络图片成功,否则失败
if statusCode==200 then
local texture=cc.Texture2D:new()
texture:initWithImage(image)
local sp_goodsItem=cc.Sprite:createWithTexture(texture) --直接创建请求的网络图片精灵,不用再保存到本地,很方便的
end
end
---最后一个参数是tag值,缺省是-1,这个参数与回调函数httpRequestCompleted的第2个参数对应
CUserData:ShareUserData():requestGoodsImageFromWeb("http://h.hiphotos.baIDu.com/zhIDao/pic/item/5bafa40f4bfbfbed0470471b78f0f736afc31fac.jpg",httpRequestCompleted,123)
c++实现如下:
voID CUserData::requestGoodsImageFromWeb(std::string strUrl,int callback,int tag)
{
if (strUrl.length()==0 || callback==0)
{
return;
}
httpRequest* request = new httpRequest();
request->setRequestType(httpRequest::Type::GET);
request->setUrl(strUrl.c_str());
request->setResponseCallback(std::bind(&CUserData::onhttpRequestCompleted_head,this,std::placeholders::_1,std::placeholders::_2,callback,tag));
httpClIEnt::getInstance()->send(request);
request->release();
}
voID CUserData::onhttpRequestCompleted_head(httpClIEnt *sender,httpResponse *response,serif; Font-size:14px; line-height:24px"> if (response && response->isSucceed())
log("response code: %ld",response->getResponseCode());
// 打印接收到的字符串
std::vector<char> *buffer = response->getResponseData();
std::string bufffff(buffer->begin(),buffer->end());
log("%s",bufffff.c_str());
if (callback!=0) {
LuaStack *stack = LuaEngine::getInstance()->getLuaStack();
stack->pushInt(200);
stack->pushInt(tag);
auto img=new Image;
img->autorelease();
img->initWithImageData((const unsigned char *)buffer->data(),buffer->size());
stack->pushObject(img,"Image");
stack->executeFunctionByHandler(callback,3); //执行lua函数
stack->clean();
}
if (response){
log("error buffer: %s",response->getErrorBuffer());
if (callback!=0) {
LuaStack *stack = LuaEngine::getInstance()->getLuaStack();
stack->pushInt(0);
stack->pushInt(tag);
stack->executeFunctionByHandler(callback,2); //执行lua函数
stack->clean();
绑定到lua的代码如下:
int lua_cocos2dx_custom__CUserData_requestGoodsImageFromWeb(lua_State* tolua_S)
int argc = 0;
cocos2d::CUserData* cobj = nullptr;
bool ok = true;
#if COCOS2D_DEBUG >= 1
tolua_Error tolua_err;
#endif
if (!tolua_isusertype(tolua_S,1,"cc.CUserData",&tolua_err)) goto tolua_lerror;
cobj = (cocos2d::CUserData*)tolua_tousertype(tolua_S,0);
if (!cobj)
tolua_error(tolua_S,"invalID 'cobj' in function 'lua_cocos2dx_custom__CUserData_requestGoodsImageFromWeb'",nullptr);
return 0;
argc = lua_gettop(tolua_S)-1;
if (argc == 2)
std::string arg0;
int arg1;
ok &= luaval_to_std_string(tolua_S,2,&arg0);
arg1=toluafix_ref_function(tolua_S,3,serif; Font-size:14px; line-height:24px"> if(!ok)
return 0;
cobj->requestGoodsImageFromWeb(arg0,arg1);
}else if (argc == 3)
int arg2;
ok &= luaval_to_int32(tolua_S,4,&arg2);
cclOG("%s has wrong number of arguments: %d,was expecting %d \n","requestGoodsImageFromWeb",argc,2);
return 0;
tolua_lerror:
tolua_error(tolua_S,"#ferror in function 'lua_cocos2dx_custom__CUserData_requestGoodsImageFromWeb'.",&tolua_err);
} 总结
以上是内存溢出为你收集整理的cocos2dx 3.x lua http请求网络图片,自己封装的,很好用,需要的可以看看全部内容,希望文章能够帮你解决cocos2dx 3.x lua http请求网络图片,自己封装的,很好用,需要的可以看看所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)