MainScene.lua
local NetSprite=require("app.scenes.NetSprite") require("app.ImageManager") local zip=require("zlib") local uncompress=zip.inflate() local url ="https://codeload.github.com/diegonehab/luasocket/zip/master" local MainScene = class("MainScene",function() return display.newScene("MainScene") end) local pathToSave = device.writablePath.."res/" -- 保存路径 local assetsManager = nil -- 资源管理器对象 function MainScene:ctor() if nil == assetsManager then -- 创建一个资源管理器,第一个参数是zip包下载地址,第二个参数是版本文件,第三个参数是保存路径 assetsManager = cc.AssetsManager:new("https://raw.github.com/samuele3hu/AssetsManagerTest/master/package.zip","https://raw.github.com/samuele3hu/AssetsManagerTest/master/version",pathToSave) -- 保留所有权,该方法会增加Ref对象的引用计数 assetsManager:retain() -- 设置一系列委托 assetsManager:setDelegate(function(event) self:onError(event) end,cc.ASSETSMANAGER_PROTOCOL_ERROR ) assetsManager:setDelegate(function(event) self:onProgress(event) end,cc.ASSETSMANAGER_PROTOCOL_PROGRESS) assetsManager:setDelegate(function(event) self:onSuccess(event) end,cc.ASSETSMANAGER_PROTOCOL_SUCCESS ) assetsManager:setConnectionTimeout(3)-- 设置连接超时 assetsManager:update() end ---下载图片并放入到压缩文件中 --local request1 = network.createHTTPRequest(function(event) self:callback1(event) end,url,"GET") --request1:start() ---登陆 -- local request = network.createHTTPRequest(function(event) self:requestCallBack(event) end,"POST") -- request:addPOSTValue("device","1") -- 添加post中的传递参数 key 和value -- request:addPOSTValue("mobileType","MI NOTE LTE") -- request:addPOSTValue("machineCode","ffffffff-d25d-d35f-6ff0-f7c4728dd8ab") -- request:start() --图片下载的初始化 --ImageCacheManager:init() end -- 进度更新回调 function MainScene:onProgress( percent ) -- 显示下载进度 local progress = string.format("downloading %d%%",percent) print(progress) end -- 下载成功方法回调 function MainScene:onSuccess() print("downloading ok") end -- 下载错误回调 function MainScene:onError(errorCode) -- 没有新版本 if errorCode == cc.ASSETSMANAGER_NO_NEW_VERSION then print("no new version") elseif errorCode == cc.ASSETSMANAGER_NETWORK then -- 网络错误 print("network error") end end function MainScene:onEnter() ---下载图片 -- ImageCacheManager:downloadImage(url,-- function(event) self:onRequestFinished(event) end ) end function MainScene:onExit() end function MainScene:onRequestFinished(params) --显示图片 -- ImageCacheManager:newCacheSprite(url,display.cx,display.cy) -- :addTo(self):align(display.CENTER,display.cy) end function MainScene:requestCallBack(event) ---登陆返回 local ok = (event.name == "completed") local request = event.request if not ok then -- 请求失败,显示错误代码和错误消息 print(request:getErrorCode(),request:getErrorMessage()) return end local code = request:getResponseStatusCode() if code ~= 200 then -- 请求结束,但没有返回 200 响应代码 print(code) return end -- 请求成功,显示服务端返回的内容 local response = request:getResponseString() print(response) local t=json.decode(response) print(t.data.signature) end function MainScene:callback1(event) --下载图片并压缩返回 local request = event.request print(event.name) if event.name == "completed" then if request:getResponseStatusCode() ~= 200 then return else print(100) request:saveResponseData("res/a.zip") end elseif event.name == "progress" then print(event.total) print(event.dltotal) print(math.ceil(event.dltotal*100/event.total)) else printf("getErrorCode() = %d,getErrorMessage() = %s",request:getErrorCode(),request:getErrorMessage()) return end end return MainScene
--[[ 2 下载图片的缓存管理器 4 ]] require "lfs" ImageCacheManager = {} local gameState = require(cc.PACKAGE_NAME .. ".cc.utils.GameState") -- 图片缓存信息表,包括下载图片URL对应的md5值和最后更新时间 local cacheData = {} -- 初始化GameState,存储缓存图片相关信息 function ImageCacheManager:init() gameState.init(function(param) -- local returnValue = nil if param.errorCode then print("error") else if param.name == "save" then elseif param.name == "load" then end end return param.values end,"image_cache.txt",nil) if io.exists(gameState.getGameStatePath()) then cacheData = gameState.load() end end --[[ 根据图片的md5值判断是否存在在缓存表中 @param 图片的md5值 @return 存在:true以及所在的位置,不存在:false ]] function ImageCacheManager:exist(md5) for i = 1,#cacheData do if cacheData[i].md5 == md5 then return true,i end end return false end --[[ 修改图片在缓存表中的最后更新时间 @param md5:图片的md5值,position:图片在缓存表中的位置 ]] function ImageCacheManager:updateCacheTime(md5,position) if cacheData[position].md5 == md5 then table.remove(cacheData,position) self:insertCacheData(md5) -- dump(cacheData,"update") else print("ImageCacheManager updateCacheTime ERROR!") end end --[[ 根据缓存目录里的图片来创建精灵 @param url 缓存图片的URL地址 x:精灵横向坐标值,y:精灵纵向坐标值 @return 若缓存表中存在对应的md5值,返回创建的精灵对象 ]] function ImageCacheManager:newCacheSprite(url,x,y) local md5 = crypto.md5(url,false) local isExist,position = self:exist(md5) if isExist then self:updateCacheTime(md5,position) local filename = device.writablePath .. "cache/" ..md5 .. ".png" return display.newSprite(filename,y) end end --[[ 向图片缓存表中插入数据 @param 图片的md5值 ]] function ImageCacheManager:insertCacheData(md5) local data = {} data.time = os.time() data.md5 = md5 if table.nums(cacheData) > 100 then -- 超过缓存上限,清除第一个元素(通常最久未更新时间戳) table.remove(cacheData,1) end table.insert(cacheData,data) gameState.save(cacheData) end --[[ 下载缓存图片 @param url 下载图片的URL地址 callback 调用下载处的回调函数地址 ]] function ImageCacheManager:downloadImage(url,callback) local md5 = crypto.md5(url,false) if not self:exist(md5) then local request = network.createHTTPRequest(function (event) local ok = (event.name == "completed") local request = event.request if not ok then print(request:getErrorCode(),request:getErrorMessage()) return end local code = request:getResponseStatusCode() if code ~= 200 then -- 请求结束,但没有返回 200 响应代码 print(code) return end local filename = md5 .. ".png" self.path = device.writablePath.."cache/" --获取本地存储目录 if not io.exists(self.path) then lfs.mkdir(self.path) --目录不存在,创建此目录 end request:saveResponseData(device.writablePath .. "cache/" .. filename) local item = {} item.time = os.time() item.md5 = md5 table.insert(cacheData,item) gameState.save(cacheData) if callback then callback(url) end end,"GET") request:start() else if callback then callback(url) end print("The %s has downloaded.",url) end end --[[ 根据需要删除缓存图片,释放存储空间 ]] function ImageCacheManager:gcCache() for i = #cacheData,1,-1 do local recordTime = cacheData[i].time local currentTime = os.time() if currentTime - recordTime > 7*24*3600 then local cmd if (device.platform == "windows") then cmd = "DEL /Q " .. device.writablePath .. "cache\\" .. cacheData[i].md5 .. ".png" else cmd = "rm -r " .. device.writablePath .. "cache/" .. cacheData[i].md5 .. ".png" end os.execute(cmd) table.remove(cacheData,i) end end gameState.save(cacheData) end原文链接:https://www.f2er.com/cocos2dx/339190.html