调用cc.RenderTexture 即可。 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 --需要截的屏幕大小 local render_texture = cc.RenderTexture: create ( 640 , 1136 ) --开始截屏 render_textu
调用cc.RenderTexture 即可。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
local render_texture = cc.RenderTexture:
create(
640,
1136)
render_texture:begin()
self.node_container:visit()
render_texture:endToLua()
local photo_texture = render_texture:getSprite():getTexture()
local sprite_photo = cc.Sprite:createWithTexture(photo_texture)
sprite_photo:flipY()
local result = render_texture:saveToFile(
"share.png", cc.IMAGE_FORMAT_PNG)
if not result then
print(
"save file failed")
end
注:若需要让某些图片强制渲染,调用sprite:visit()方法。
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
bool RenderTexture
::saveToFile(const std
::string& fileName, Image
::Format format, bool isRGBA)
{
CCASSERT(format
== Image
::Format::JPG || format
== Image
::Format::PNG,
"the image can only be saved as JPG or PNG format");
if (isRGBA
&& format
== Image
::Format::JPG) CCLOG(
"RGBA is not supported for JPG format");
std
::string fullpath
= FileUtils
::getInstance()
->getWritablePath()
+ fileName;
_saveToFileCommand
.init(_globalZOrder);
_saveToFileCommand
.func
= CC_CALLBACK_0(RenderTexture
::onSaveToFile, this, fullpath, isRGBA);
Director
::getInstance()
->getRenderer()
->addCommand(
&_saveToFileCommand);
return true;
}