保存图片到本地:
--[[
下面代码的意思是将大图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)