实现这个功能时候,在网上查了一些方法,http://www.cocoachina.com/bbs/read.PHP?tid-277669-page-1.html这个论坛里面有个人用renderTexture->setVirtualViewport()来实现,没看懂(后面慢慢研究),看了一些回复后,自己想了个笨方法,如下:
auto winSize = Director::getInstance()->getWinSize();
auto origin = Director::getInstance()->getVisibleOrigin();
RenderTexture *renderTexture = RenderTexture::create(winSize.width,winSize.height);
//这里截取整个屏幕
renderTexture->begin();
Director::getInstance()->getRunningScene()->visit();
renderTexture->end();
//截取的指定区域
Rect shootArea(winSize.width - 200,winSize.height - 200,200,200);
auto sp = renderTexture->getSprite();
sp->setTextureRect(shootArea);
//显示截取要的部分
//sp->removeFromParent();
//this->addChild(sp,222);
sp->setPosition(sp->getContentSize()/2);
RenderTexture *renderTexture2 = RenderTexture::create(shootArea.size.width,shootArea.size.height);
renderTexture2->begin();
sp->visit();
renderTexture2->end();
if (renderTexture2->saveToFile("screen_shot.png",Image::Format::PNG))
{
//保存在这个路径FileUtils::sharedFileUtils()->getWritablePath()
CCLOG("screenshot success");
}
else
{
CCLOG("screenshot Failed");
}
谁有其他更好的方法说一下
原文链接:https://www.f2er.com/cocos2dx/341070.html