W3Cschool
恭喜您成為首批注冊用戶
獲得88經(jīng)驗值獎勵
本節(jié)貼出如何通過http請求網(wǎng)絡(luò)圖片,并繪制到屏幕上
發(fā)送請求
/**
編譯到Android平臺之后注意添加聯(lián)網(wǎng)權(quán)限
**/
CCHttpRequest* request = new CCHttpRequest();
// 請求圖片的url
request->setUrl("http://www.9miao.com/template/dean_hotspot_141011/deancss/logo.png");
//請求方式
request->setRequestType(CCHttpRequest::kHttpGet);
//設(shè)置回調(diào)
request->setResponseCallback(this, httpresponse_selector(FirstViewController::onHttpRequestImageCompleted));
// 設(shè)置tag
request->setTag("image");
CCHttpClient::getInstance()->send(request);
request->release();
接受圖片
void FirstViewController::onHttpRequestImageCompleted(CCHttpClient *sender, CCHttpResponse *response)
{
if (!response)
{
return;
}
if (!response->isSucceed())
{
CCLog("response failed");
CCLog("error buffer: %s", response->getErrorBuffer());
return;
}
vector<char> *buffer = response->getResponseData();
//創(chuàng)建一個CCImage
CCImage* img = new CCImage();
img->initWithImageData((unsigned char*)buffer->data(), buffer->size());
//創(chuàng)建一個紋理CAImage
CAImage* texture = new CAImage();
bool isImg = texture->initWithImage(img);
img->release();
//通過CAImageView繪制到屏幕
CAImageView* iv = CAImageView::createWithImage(texture);
iv->setFrame(DRect(0, 0, 200, 200));
this->getView()->addSubview(iv);
}
如果成功,將會看到9秒社團的logo。
Copyright©2021 w3cschool編程獅|閩ICP備15016281號-3|閩公網(wǎng)安備35020302033924號
違法和不良信息舉報電話:173-0602-2364|舉報郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號
聯(lián)系方式:
更多建議: