当前位置:网站首页>Lua wechat avatar URL

Lua wechat avatar URL

2022-07-05 04:56:00 Meteor spot

function App.createHeadSprite(url,userID)

    local name = md5.sumhexa(url)
    local headPicName = name..".png"
    local filepath = cc.FileUtils:getInstance():getWritablePath()
    filepath = filepath.."UserFaceImage"
    local imgFile = filepath.."/"..headPicName
    if App.fileExist(imgFile) then
        ls.TextureCache:removeTextureForKey(imgFile)
        ls.TextureCache:addImage(imgFile)
        return;
    end


    if url ~= nil and string.len(url) > 0 then
        App.downHeadByUrl(url,userID,updateHeadImage)
    end
end

-- Download the avatar
function App.downHeadByUrl(url,userID)
    if url == nil then
        return
    end
    -- Store names
    local name = md5.sumhexa(url)
    local headPicName = name..".png"
    -- Storage path
    local filepath = cc.FileUtils:getInstance():getWritablePath()

    filepath = filepath.."UserFaceImage"
    ls.FileUtils:createDirectory(filepath)
    --end
    -- Downloaded avatar pictures
    local imgFile = filepath.."/"..headPicName
    local xhr = cc.XMLHttpRequest:new()
    xhr.responseType = cc.XMLHTTPREQUEST_RESPONSE_ARRAY_BUFFER
    xhr:open("GET",url)

    local function OnDownLoadFinish()
        if xhr.readyState == 4 and (xhr.status >= 200 and xhr.status < 207) then
            local fileData = xhr.response
            local size = table.getn(fileData)
            if size > 100 then
                --os.remove(imgFile)
                local file = io.open(imgFile,"wb")
                if file==nil then 
                    return 
                end 
                for i = 1,size do
                    file:write(string.char(fileData[i]))
                end
                file:close()        
            end
            --local texture = ls.TextureCache:addImage(imgFile)
            ls.TextureCache:addImage(imgFile)
            ls.dispatchCustomEvent(App.Event.updataWXHead, userID)
            xhr:unregisterScriptHandler()
        end
    end
    xhr:registerScriptHandler(OnDownLoadFinish)
    xhr:send()
end
--------------------- 

-- Get avatar texture
function App.getUserFaceImage(userID , url)
    -- Store names
    local name = md5.sumhexa(url)
    local headPicName = name..".png"
    -- Storage path
    local filepath = cc.FileUtils:getInstance():getWritablePath()

    filepath = filepath.."UserFaceImage"
    --end
    -- Downloaded avatar pictures
    local imgFile = filepath.."/"..headPicName
    local texture = ls.TextureCache:getTextureForKey(imgFile)
    if not texture then
        local pTemp = cc.Sprite:create("lobby_female_face.png")
        texture = pTemp:getTexture()
    end
    return texture
end
---------------------

-- Judge whether the file exists
function App.fileExist(path)
    if path == nil then
        return false
    end

    local file = io.open(path,"rb")
    if file ~= nil then
        file:close()
        return true
    end

    return false
end
--------------------- 

原网站

版权声明
本文为[Meteor spot]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140625238061.html