头像采用url的处理

这是一个用Lua编写的WebSprite类,用于处理以URL形式提供的头像。该类创建一个精灵节点,并根据传入的宽度和高度参数调整大小。它会检查本地缓存中是否存在图片文件,如果存在则直接加载,否则从网络下载并在下载完成后显示。
-- 获取头像的地址
            local sHeadUrl = self._ResultInfoTable.tbUserPoint[tostring(realSeat)].sHeadUrl;
            if sHeadUrl and sHeadUrl ~= "" then
                --创建头像
                local headSp = WebSprite.create(sHeadUrl, 60, 60);
                headNode:addChild(headSp);
            else
                -- 创建默认头像
                local defaultHeadSp = cc.Sprite:create("这里使用默认头像");
                defaultHeadSp:setPosition(0, -5);
                headNode:addChild(defaultHeadSp);

            end


-----------------------------WebSprite.lua--------------------------------------


local WebSprite = class("WebSprite",function()
    return cc.Sprite:create()
end)


function WebSprite.create(imgurlPath,fWidth,fHeight)
    local pSpr = WebSprite.new()
    pSpr:createContent(imgurlPath,fWidth,fHeight)
    return pSpr
end


function WebSprite:ctor()
    self.m_sImgurlPath = ""
    self.m_fWidth = nil
    self.m_fHeight = nil
    self.m_LoadingSpr = nil
    self.pNodeStenBG = nil
end


function WebSprite:createContent(imgurlPath,fWidth,fHeight)
    self.m_sImgurlPath = imgurlPath


    if fWidth ~= nil and fHeight ~= nil then 
        self.m_fWidth = fWidth
        self.m_fHeight = fHeight  
    end


    local ThefWidth = self.m_fWidth
    local ThefHeight = self.m_fHeight


    ---------添加黑色的层
--    self.pNodeStenBG = cc.LayerColor:create(cc.c4b(0,0,0,255))
--    if self.m_fWidth ~= nil and self.m_fHeight ~= nil then
--        self.pNodeStenBG:setContentSize(cc.size(self.m_fWidth,self.m_fHeight))
--    else
--        self.pNodeStenBG:setContentSize(cc.size(150,150))
--    end


--    self:addChild(self.pNodeStenBG)


    ------加载中的图片
--    self.m_LoadingSpr = cc.Sprite:createWithSpriteFrameName("websprite_progress.png")
--    if self.m_fWidth ~= nil and self.m_fHeight ~= nil then
--        self.m_LoadingSpr:setPosition(cc.p(ThefWidth*0.5,ThefHeight*0.5))
--    else
--        self.m_LoadingSpr:setPosition(cc.p(75,75))
--    end
--    self:addChild(self.m_LoadingSpr)


--    local pAction = cc.RepeatForever:create(cc.RotateBy:create(1,360))
--    self.m_LoadingSpr:runAction(pAction)
    -------------


    if self.m_fWidth ~= nil and self.m_fHeight ~= nil then
        self:setContentSize(cc.size(ThefWidth,ThefHeight))
    end


    --
    local fileName = "";
    fileName = ToMD5(self.m_sImgurlPath, string.len(self.m_sImgurlPath))


    -- while string.find(fileName,"/") ~= nil do
    --     local fileName_s,fileName_e = string.find(fileName,"/")
    --     fileName = string.sub(fileName, fileName_s+1)
    -- end


    local strWritablePath = cc.FileUtils:getInstance():getWritablePath()
    strWritablePath = strWritablePath .. "cachePath/cleanResPath/"


    local function DownLoadOverbackFunc(pData)
--        self:removeChild(self.pNodeStenBG)
--        self:removeChild(self.m_LoadingSpr)


        local sFilePath = pData["ConpletePathName"];
        local pSprCon = nil;
        if string.len(sFilePath) > 0 then
            pSprCon = cc.Sprite:create(pData["ConpletePathName"]);
        end


        if pSprCon then
            if self.m_fWidth ~= nil and self.m_fHeight ~= nil then
                pSprCon:setScale(ThefWidth/pSprCon:getContentSize().width,ThefHeight/pSprCon:getContentSize().height)
                pSprCon:setPosition(cc.p(ThefWidth*0.5,ThefHeight*0.5))
            else
                pSprCon:setPosition(cc.p(75,75))
                self:setContentSize(pSprCon:getContentSize())
            end
            self:addChild(pSprCon)
        end
    end


    local bFileIsHave = cc.FileUtils:getInstance():isFileExist(strWritablePath .. fileName)
    print("isHave:", bFileIsHave)


    local pSprCon = nil;
    if bFileIsHave then
        pSprCon = cc.Sprite:create(strWritablePath .. fileName);
    end


    if pSprCon then 
        --print("===",strWritablePath .. fileName)
--        self:removeChild(self.pNodeStenBG)
--        self:removeChild(self.m_LoadingSpr)
        if self.m_fWidth ~= nil and self.m_fHeight ~= nil then
            pSprCon:setScale(ThefWidth/pSprCon:getContentSize().width,ThefHeight/pSprCon:getContentSize().height)
            pSprCon:setPosition(cc.p(ThefWidth*0.5,ThefHeight*0.5))
        else
            pSprCon:setPosition(cc.p(75,75))
            self:setContentSize(pSprCon:getContentSize())
        end
        self:addChild(pSprCon)
    else
        local pWebFile = CWebFile:Create(self.m_sImgurlPath, "cachePath/cleanResPath", true)
        pWebFile:SetHandler(DownLoadOverbackFunc)
    end
end


return WebSprite



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值