cocos2d screenshot function

Screenshot function

    var size = cc.director.getWinSize();
    var width = size.width;
    var height = size.height;
//Add screenshot function
    var renderTexture =new cc.RenderTexture(parseInt(width) , parseInt(height) , cc.Texture2D.PIXEL_FORMAT_RGBA8888);
    renderTexture.setPosition(size.width / 2, size.height / 2);
    renderTexture . begin () ;            // start screen capture

    this . mCurrenTopicLayout . visit () ; // Traverse all the child node information of the current scene Scene and draw it into the screen
    renderTexture . end () ;              //end screen capture
    var imageUrl = "topic_" + mTopicLayerSelf.mTopicPos + ".png";
//Save the image to the running device
    var result = renderTexture.saveToFile(imageUrl);
//将截图的缓存清理掉。否则会造成 下次截图的时候 会将上次的截图一并截取出来
    renderTexture.cleanup();
//拿到图片缓存的路径
    var imgPath = jsb.fileUtils.getWritablePath();
    if (imgPath.length == 0) {
        return;
    }
    if (result) {
        imgPath += imageUrl;
     }





//删除本地的图片
jsb.fileUtils.removeFile(imgPath,function () {
    cc.log("删除图片");
});
删除本地截图以后。要将本地纹理缓存同时清空。cocos2d会自动将精灵图片存入纹理缓存中。以便下次使用
cc.spriteFrameCache.removeSpriteFrames();
cc.textureCache.removeAllTextures();

cocos2d-x 功能类似

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325989000&siteId=291194637