HeadImageSprite.lua 3.96 KB
cc.exports.HeadImageSprite = class("HeadImageSprite",function()
	return display.newSprite()
end)

function HeadImageSprite: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;
	
    local function onNodeEventHandler(event)
        if event == "exit" then
            if self.xhr then
                self.xhr:unregisterScriptHandler();
                self.xhr = nil;
            end
        end
    end
    self:registerScriptHandler(onNodeEventHandler);

    self:createSprite();
end

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

--将下载的文件保存到本地
function HeadImageSprite: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 HeadImageSprite: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.5,0.5));
    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("HeadImageSprite SCALE:"..scale);
    else
        self.width = spritew;
        self.height = spriteh;
    end
    if self.completeCallback then
        self.completeCallback(self);
    end
end

return HeadImageSprite