保存图片到相册

 

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

标签: cocos2d-xlua截屏保存图片沙盒
 分类:

保存图片到本地:

--[[

下面代码的意思是将大图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)

 

 

 

 

 

 

 

 

 


local writablePath = cc.FileUtils:getInstance():getWritablePath()
print("writablePath is %s", writablePath)
local bgImage = display.newSprite("bg_login.jpg"):align(display.CENTER, display.cx, display.cy)
local sizerr = bgImage:getContentSize()

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

bgImage:setPosition(cc.p(sizerr.width / 2, sizerr.height / 2))

render:begin()

bgImage:visit()

render:endToLua()

render:saveToFile("Image_richeng_bg", cc.IMAGE_FORMAT_PNG) --写到文件

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

posted @ 2016-12-09 10:27  一个橙子~  阅读(1087)  评论(0编辑  收藏  举报