cocos2dx-lua3.12版本保存图片、截屏显示

前端之家收集整理的这篇文章主要介绍了cocos2dx-lua3.12版本保存图片、截屏显示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

保存图片到本地:

--[[

下面代码的意思是将大图plist文件中的分离出来,并保存到沙盒目录下

]]

local size = cc.Director:getInstance():getVisibleSize()

local plistFile = "" --你的plist路径

local pngFile = "" --你的大图路径

cc.SpriteFrameCache:getInstance():addSpriteFrames(plistFile) --加载图片进内存

local vectormap = cc.FileUtils:getInstance():getValueMapFromFile(plistFile) --读取plist

local frames = vectormap.frames

local writablePath = cc.FileUtils:getInstance():getWritablePath()

print("writablePath is %s",writablePath)

for key,value in pairs(frames) do

local str = key --获得plist内的图片

local spr = cc.Sprite:createWithSpriteFrameName(str)

local size = spr:getContentSize()

local render = cc.RenderTexture:create(size.width,size.height)

spr:setPosition(cc.p(size.width / 2,size.height / 2))

render:begin()

spr:visit()

render:endToLua()

ender:saveToFile(str,cc.IMAGE_FORMAT_PNG) --写到文件

print("save " .. str .. " to ".. writablePath .. str)

end


截屏显示

local size = cc.Director:getInstance():getVisibleSize()

local function afterCaptured(succeed,outputFile)

if succeed then

local outputSprite = cc.Sprite:create(outputFile)

outputSprite:setPosition(cc.p(size.width/2,size.height/2))

self:addChild(outputSprite,10)

outputSprite:setScale(0.5)

else

print("Capture screen Failed.")

end

end


local screen = cc.MenuItemFont:create("SCREEN")

screen:setPosition(cc.p(size.width-100,size.height-100))

local menu = cc.Menu:create(screen)

menu:setPosition(cc.p(0,0))

menu:setAnchorPoint(cc.p(0,0))

self:addChild(menu)

local function menuCallBack(pSender)

cc.utils:captureScreen(afterCaptured,"screen.png")

end

screen:registerScriptTapHandler(menuCallBack)

猜你在找的Cocos2d-x相关文章