codecamp

http请求网络图

本节贴出如何通过http请求网络图片,并绘制到屏幕上


发送请求

    /**
    编译到Android平台之后注意添加联网权限
    **/
    CCHttpRequest* request = new CCHttpRequest();
     
    // 请求图片的url
    request->setUrl("http://www.9miao.com/template/dean_hotspot_141011/deancss/logo.png");
     
    //请求方式
    request->setRequestType(CCHttpRequest::kHttpGet);
     
    //设置回调
    request->setResponseCallback(this, httpresponse_selector(FirstViewController::onHttpRequestImageCompleted));
     
    // 设置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();
     
    //创建一个CCImage
    CCImage* img = new CCImage();
     
    img->initWithImageData((unsigned char*)buffer->data(), buffer->size());
     
    //创建一个纹理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。



网络通信之http
CAViewAnimation(动画实现)
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

四、调用系统支持

八、宏定义

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }