cocos2d-lua屏幕截屏的方法,超好用,亲测可行

本文介绍了cocos2d-x lua引擎中如何实现屏幕截屏,包括系统自带的截屏功能和一帧内截图并保存纹理到本地的详细步骤,经实际测试有效。
摘要由CSDN通过智能技术生成

cocos2d-x lua系统自带截屏功能,使用方便。代码如下:

 local fileName = "printScreen.png"
        -- 移除纹理缓存
        cc.Director:getInstance():getTextureCache():removeTextureForKey(fileName)
        self:removeChildByTag(1000)
        -- 截屏
        cc.utils:captureScreen(function(succeed, outputFile)
            if succeed then
              local winSize = cc.Director:getInstance():getWinSize()
              local sp = cc.Sprite:create(outputFile)
              self:addChild(sp, 0, 1000)
              sp:setPosition(winSize.width / 2, winSize.height / 2)
              sp:setScale(0.5) -- 显示缩放
                print(outputFile)
            else
                cc.showTextTips("截屏失败")
            end
        end, fileName)


第二种方法,一帧之内进行截图并把纹理保存到本地的方法

关键代码

function WeixinShareTips.createWeixinImageFile(bShareCurrentScene, func_next)

    local imgSize = cc.size(640, 960)
    local backGround = {
        path = "weixin/background_twoDimension.jpg", 
        pos = cc.p(imgSize.width / 2, imgSize.height / 2),
        scale = 1
    }
    local logo = {
        path = cc.logos[cc.getSDKPlat()], 
        pos = cc.p(503, 848),
        scale = 0.51
    }
    local twoDimension = {
        path = nil, 
        pos = cc.p(532, 106),
        scale = 1
    }

    if bShareCurrentScene then
        backGround = nil
        logo.pos = cc.p(117, 550)
        twoDimension = nil
        imgSize = cc.size(960, 640)
        logo.scale = 0.3
    elseif cc.getSDKPlat() == "ios_yd" then
        twoDimension.path = "weixin/twoDimension_bierangwomaoxian.jpg"
    elseif cc.getSDKPlat() == "ios_yd2" then
        twoDimension.path = "weixin/twoDimension_maoxianqishituan.jpg"
    elseif cc.getSDKPlat() == "ios_yd3" then
        twoDimension.path = "weixin/twoDimension_menghuanqishituan.jpg"
    else
        backGround.path = "weixin/background_noDimension.jpg"
        twoDimension = nil
    end

    local bigImagePath = WeixinShareTips.createWeixinImageFileWithLogoAndTwoDimension("bigWeixinShare.jpg", backGround, logo, twoDimension, imgSize)
    local saveRet = bigImagePath ~= nil

    local function delayDoSomething(call_todo)
        cc.Director : getInstance() : getRunningScene()  : runAction(cc.Sequence:create(
        cc.DelayTime:create(0.1), cc.CallFunc:create(function ()
            call_todo()
        end)))
    end

    delayDoSomething(function()
        local imgScale = 1/8
        imgSize = cc.size(imgSize.width * imgScale, imgSize.height * imgScale)
        backGround = {
            path = bigImagePath, 
            pos = cc.p(imgSize.width / 2, imgSize.height / 2),
            scale = imgScale
        }

        local smallImagePath = WeixinShareTips.createWeixinImageFileWithLogoAndTwoDimension("smallWeixinShare.jpg", backGround, nil, nil, imgSize)
        delayDoSomething(function()
            saveRet = saveRet and (smallImagePath ~= nil)
            func_next(saveRet, smallImagePath, bigImagePath)
        end)
    end)
end

function WeixinShareTips.createWeixinImageFileWithLogoAndTwoDimension(toFileName, backGround, logo, twoDimension, imgSize)

    local function createRenderNodeWithPathPos(pathPos)
        local sprite = nil
        if pathPos then
            sprite = cc.Sprite:create(pathPos.path)
            sprite : setPosition(pathPos.pos)
            sprite : setScale(pathPos.scale)
        end
        return sprite
    end

    local function createRenderTextureWithNodes(logoRenderNode, twoDimensionNode, backGroundNode)
          -- body
        local renderTexture = cc.RenderTexture:create(imgSize.width, imgSize.height)

        renderTexture : beginWithClear(0,0,0,0)

        if backGroundNode and (cc.Director:getInstance():getRunningScene() ~= backGroundNode) then
            backGro
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值