截取屏幕(考虑分辨率适配)

来源:互联网 发布:装修设计实用软件 编辑:程序博客网 时间:2024/06/16 07:28

1.截取屏幕时取图片实际大小

function MatchWinDialog:screenshotShareToWX()   local winSize = cc.Director:getInstance():getWinSize()   local bg = gt.seekNodeByName(self.rootNode, "Image_bg")   local layerSize = cc.size(985, 535)   local dx = winSize.width/2 - 491   local dy = winSize.height/2 - 227   local width, height = layerSize.width, layerSize.height   local screenshot = cc.RenderTexture:create(winSize.width, winSize.height)   screenshot:begin()   self.rootNode:visit()   screenshot:endToLua()   local spShot = cc.RenderTexture:create(width, height)   local tempSp = cc.Sprite:createWithTexture(screenshot:getSprite():getTexture(), cc.rect(dx, dy, width, height))   tempSp:setAnchorPoint(0, 0)   tempSp:setPosition(0, 0)   tempSp:setFlippedY(true)   spShot:begin()   tempSp:visit()   spShot:endToLua()   local screenshotFileName = string.format("wx-%s.jpg", os.date("%Y-%m-%d_%H:%M:%S", os.time()))   spShot:saveToFile(screenshotFileName, cc.IMAGE_FORMAT_JPEG, false)   self.shareImgFilePath = cc.FileUtils:getInstance():getWritablePath() .. screenshotFileName   self.scheduleHandler = gt.scheduler:scheduleScriptFunc(handler(self, self.update), 0, false)end
2.考虑左下脚,和大小
function MatchWinDialog:screenshotShareToWX()   local winSize = cc.Director:getInstance():getWinSize()   local bg = gt.seekNodeByName(self.rootNode, "Image_bg")   local layerSize = bg:getContentSize()   local pos = bg:getParent():convertToWorldSpace(cc.p(bg:getPositionX(), bg:getPositionY()))   --获取成世界坐标就行了   local dx = pos.x - layerSize.width/2   local dy = pos.y - layerSize.height/2   local width, height = layerSize.width, layerSize.height      local screenshot = cc.RenderTexture:create(winSize.width, winSize.height)   screenshot:begin()   self.rootNode:visit()   screenshot:endToLua()   local spShot = cc.RenderTexture:create(width, height)   local tempSp = cc.Sprite:createWithTexture(screenshot:getSprite():getTexture(), cc.rect(dx, dy, width, height))   tempSp:setAnchorPoint(0, 0)   tempSp:setPosition(0, 0)   tempSp:setFlippedY(true)   spShot:begin()   tempSp:visit()   spShot:endToLua()   local screenshotFileName = string.format("wx-%s.jpg", os.date("%Y-%m-%d_%H:%M:%S", os.time()))   spShot:saveToFile(screenshotFileName, cc.IMAGE_FORMAT_JPEG, false)   self.shareImgFilePath = cc.FileUtils:getInstance():getWritablePath() .. screenshotFileName   self.scheduleHandler = gt.scheduler:scheduleScriptFunc(handler(self, self.update), 0, false)end


不断更新检测图片是否存在

function MatchWinDialog:update()    if self.shareImgFilePath and cc.FileUtils:getInstance():isFileExist(self.shareImgFilePath) then        gt.scheduler:unscheduleScriptEntry(self.scheduleHandler)        local shareBtn = gt.seekNodeByName(self.rootNode, "Button_share")        shareBtn:setEnabled(true)        local platform = gt.SharePlatform.WechatSession        if Tools.checkVersion("1.9.0") then          platform = gt.SharePlatform.WechatTimeline        end        gt.shareImage(self.shareImgFilePath, platform)        self.shareImgFilePath = nil    endend


原创粉丝点击