http請求網(wǎng)絡(luò)圖

2018-09-08 17:57 更新

本節(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。



以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號