• coco2dx加载网络图片并保存


    直接上代码

    bool HelloWorld::init()
    {
        //////////////////////////////
        // 1. super init first
        if ( !Layer::init() )
        {
            return false;
        }
    
        HttpRequest* request = new HttpRequest();
        request->setUrl("http://neoimaging.beareyes.com.cn/png2/ni_png_2_1518.png");
        request->setRequestType(HttpRequest::Type::GET);
        request->setResponseCallback(this, httpresponse_selector(HelloWorld::onHttpRequestCompleted));
        request->setTag("GET test1");
        HttpClient::getInstance()->send(request);
        request->release();
    
        return true;
    }
    
    void HelloWorld::onHttpRequestCompleted(HttpClient *sender, HttpResponse *response)
    {
        if (!response)
        {
            return;
        }
        
        // You can get original request type from: response->request->reqType
        if (0 != strlen(response->getHttpRequest()->getTag())) 
        {
            log("%s completed", response->getHttpRequest()->getTag());
        }
        int statusCode = response->getResponseCode();
        if (!response->isSucceed()) 
        {
            log("response failed");
            log("error buffer: %s", response->getErrorBuffer());
            return;
        }
    
        //get data
        std::vector<char> *buffer = response->getResponseData();
    
        //create image
        CCImage* img = new CCImage;
        img->initWithImageData((unsigned char*)buffer->data(),buffer->size());
        
        //create texture
        cocos2d::CCTexture2D* texture = new CCTexture2D();
        bool isImg = texture->initWithImage(img);
        img->release();
    
        //create sprite
        CCSprite* sprite = CCSprite::createWithTexture(texture);
        texture->release();
        sprite->setPosition(ccp(100,100));
        addChild(sprite,100);
    
        //write file
        std::string path = CCFileUtils::sharedFileUtils()->getWritablePath();
        path+="download_image.png";
    
        FILE *fp = fopen(path.c_str(), "wb+");
        fwrite(buffer->data(), 1,buffer->size(), fp);
        fclose(fp);
    }
  • 相关阅读:
    MyBatis热部署
    FreeMarker速查手册
    SpringBoot从Eclipse添加的Tomcat容器中启动
    Delphi中的GetEnumName和GetEnumValue的使用方法
    [数据库连接字符串] Access 连接字符串
    [数据库连接字符串] Access 连接字符串
    Ms SQLServer中的Union和Union All的使用方法和区别
    Ms SQLServer中的Union和Union All的使用方法和区别
    RasAPI函数实现PPPOE拨号
    RasAPI函数实现PPPOE拨号
  • 原文地址:https://www.cnblogs.com/mrblue/p/3414808.html
Copyright © 2020-2023  润新知