---把某個節點渲染並保存為圖片 ---@param node Node 需要保存的節點 ---@param imageName string|nil 圖片名字 ---@param callback function|nil 成功回調 ---@return void function TestSaveImage(node, imageName, callback ) if tolua.isnull(node) then return end local dirPath = cc.FileUtils:getInstance():getWritablePath() .. "imgName" imageName = imgName or "image-" .. os.date("%Y%m%d%H%M"); local path = string.format("%s/%s.png", dirPath, imageName) if cc.FileUtils:getInstance():isFileExist(path) then if callback then callback(path) else callback(false) end return end local size = node:getContentSize() local renderTexture = cc.RenderTexture:create(size.width, size.height,cc.TEXTURE2_D_PIXEL_FORMAT_RGB_A8888) local ap = node:getAnchorPoint() local p = cc.p(node:getPosition()) node:setVisible(true) --設置位置到左下角 node:setPosition(cc.p(ap.x * size.width ,ap.y * size.height)) renderTexture:begin() node:visit() renderTexture:endToLua() renderTexture:retain() performWithDelay(node,function() if not cc.FileUtils:getInstance():isDirectoryExist(dirPath) then cc.FileUtils:getInstance():createDirectory(dirPath) end node:setPosition(p) renderTexture:newImage():saveToFile(savePath, false) renderTexture:release() if callback then callback(savePath) end end,0.01) end