NetImage.lua 3.49 KB
local NetImage = class("NetImage",function()
	return display.newSprite()
end)

function NetImage:ctor(url,w,h,completeCallback)
    self.writeRootPath = cc.FileUtils:getInstance():getWritablePath();
	self.path = self.writeRootPath.."netImageCache/"; --获取本地存储目录
--    if not cc.FileUtils:getInstance():isDirectoryExist(self.path) then 
--        cc.FileUtils:getInstance():createDirectory(self.path)
--    end
	self.url = url;
	self.width = w or 0;
	self.height = h or 0;
    self.completeCallback = completeCallback;
	self:createSprite()
end

function NetImage:getLocalPath()
    local url_md5 = md5(self.url);
    local picPath = self.path..url_md5..".png";
--    if cc.FileUtils:getInstance():isFileExist(picPath) then
--        return true,picPath --存在,返回本地存储文件完整路径
--    end
	return false,picPath; --不存在,返回将要存储的文件路径备用
end

function NetImage:createSprite()
    local isExist,filePath = self:getLocalPath()
	if isExist then --如果存在,直接更新纹理
        self:updateTexture(filePath);
	else --如果不存在,启动http下载
        local xhr = cc.XMLHttpRequest:new()
        xhr.responseType = 0;
        xhr:open("GET", self.url)
        local function onDownloadImage()
            print("xhr.readyState is:", xhr.readyState, "xhr.status is: ", xhr.status)
            if xhr.readyState == 4 and (xhr.status >= 200 and xhr.status < 207) then
                local fileData = xhr.response;
                self:saveFile(filePath,fileData);
            end
        end
        xhr:registerScriptHandler(onDownloadImage)
        xhr:send();
	end
end

--将下载的文件保存到本地
function NetImage:saveFile(fullFileName,fileData)
    local tempResName = fullFileName;
    local maxLength = string.len(tempResName);
    local directoryLen = 0;
    local tag = string.find(tempResName,'/');
    while tag do
        if tag ~= 1 then
            directoryLen = directoryLen + tag;
        end
        tempResName = string.sub(tempResName,tag + 1,maxLength)
        tag = string.find(tempResName,'/')
    end
    
    if directoryLen ~= 0 then
        local fullDirectory = string.sub(fullFileName,1,directoryLen)
        if not (cc.FileUtils:getInstance():isDirectoryExist(fullDirectory)) then         
            cc.FileUtils:getInstance():createDirectory(fullDirectory);
        end
    end
    
    local file = io.open(fullFileName,"wb");
    file:write(fileData);
    file:close();
    self:updateTexture(fullFileName);
end

function NetImage:updateTexture(fullFileName)
    local sprite = display.newSprite(fullFileName) --创建下载成功的sprite
	if not sprite then return end
    local spritew,spriteh = sprite:getContentSize().width,sprite:getContentSize().height;
    sprite:setAnchorPoint(cc.p(0,0));
    self:addChild(sprite);
    local scale = 1;
    if self.width > 0 and self.height > 0 then
        if spritew/spriteh > self.width/self.height then
            scale = self.width/spritew;
            sprite:setScale(self.width/spritew);
            local y = (self.height - spriteh*scale)/2;
            sprite:setPositionY(y);
        else
            scale = self.height/spriteh;
            sprite:setScale(self.height/spriteh);
            local x = (self.width - spritew*scale)/2;
            sprite:setPositionY(x);
        end
        print("NetImage SCALE:"..scale);
    else
        self.width = spritew;
        self.height = spriteh;
    end
    if self.completeCallback then
        self.completeCallback(self);
    end
end

return NetImage