cocos2dx 3.x 保存sprite上的图片

首先是cocos的截图保存功能,亲测有效:

auto pic = utils::captureNode(Director::getInstance()->getRunningScene());
time_t customTime = time(NULL);
 
std::string filePath;
#if CC_PLATFORM_ANDROID==CC_TARGET_PLATFORM
	filePath = "/sdcard/DCIM/Camera/" + StringUtils::toString(customTime) + ".jpg";
	pic->saveToFile(filePath);
#else
	filePath = FileUtils::getInstance()->getWritablePath() + StringUtils::toString(customTime) + ".jpg";
	pic->saveToFile(filePath);
#endif
Operator::requestChannel("sysmodule", "notifyScreenShot", filePath);

接下来是sprite以及他的子类的图片的保存:

Android代码:

void saveSprite(Sprite* _spr){
    Sprite* pNewSpr = Sprite::createWithSpriteFrame(_spr->getSpriteFrame());
    pNewSpr->setAnchorPoint(Vec2::ZERO);
    RenderTexture* pRender = RenderTexture::create(pNewSpr->getContentSize().width, pNewSpr->getContentSize().height, Texture2D::PixelFormat::RGBA8888);
    pRender->begin();
    _spr->visit();
    pRender->end();
    
    std::string filePath = "/CropImage/test_spr.png";
    pRender->saveToFileInAndroidSDCard(filePath,Image::Format::PNG);
}

 

你可能感兴趣的:(cocos2dx)