首页 > 代码库 > Cocos2dx网络读取图片
Cocos2dx网络读取图片
//// Connection.h// XXDemo//// Created by LeeHonGee on 14-9-4.////#ifndef __XXDemo__Connection__#define __XXDemo__Connection__#include <iostream>#include "cocos2d.h"#include "network/HttpClient.h"USING_NS_CC;using namespace std;using namespace cocos2d::network;class Connection{ public: void doHttpRequest(const char* url,const char* requestData,const char* tag,const ccHttpRequestCallback& callback); void onHttpRequestImageCompleted(HttpClient *sender, HttpResponse *response); private: //获取请求网络图片资源 Texture2D* texture;};#endif /* defined(__XXDemo__Connection__) */
//// Connection.cpp// XXXDemo//// Created by LeeHonGee on 14-9-4.////#include "Connection.h"/** 编译到Android平台之后注意添加联网权限 **/void Connection::doHttpRequest(const char* url,const char* requestData,const char* tag,const ccHttpRequestCallback& callback){ HttpRequest* request = new HttpRequest(); request->setUrl(url); request->setRequestType(HttpRequest::Type::POST); //这是回调对象和回调函数 request->setResponseCallback(callback); request->setTag(tag); //请求的数据 if(strlen(requestData)!=0){ request->setRequestData(requestData,strlen(requestData)); } HttpClient::getInstance()->send(request); //释放内存 request->release(); }void Connection::onHttpRequestImageCompleted(HttpClient *sender, HttpResponse *response){ if (!response) { return; } // You can get original request type from: response->request->reqType const char* tag = response->getHttpRequest()->getTag(); if (0 != strlen(tag)) { log("%s completed", response->getHttpRequest()->getTag()); } if (!response->isSucceed()) { log("error buffer: %s", response->getErrorBuffer()); return; } std::vector<char> *buffer = response->getResponseData(); //create image Image* image = new Image; image->initWithImageData((unsigned char*)buffer->data(),buffer->size()); //create texture texture = new Texture2D(); bool isImage = texture->initWithImage(image); if(isImage){ log("Texture2D 初始化!"); } image->release(); }
Cocos2dx网络读取图片
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。