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);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章