HeadImageSprite.lua
3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
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