-
Notifications
You must be signed in to change notification settings - Fork 33
/
HGM.FMX.ImageLoader.pas
253 lines (227 loc) · 5.27 KB
/
HGM.FMX.ImageLoader.pas
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
unit HGM.FMX.ImageLoader;
interface
uses
System.Classes, System.Types, System.SysUtils, System.Generics.Collections,
System.Threading, FMX.ImgList, FMX.Graphics;
type
TPreview = class(TBitmap)
public
Loading: Boolean;
URL: string;
ImageName: string;
end;
TPreviews = class(TObjectList<TPreview>)
private
class var
Instance: TPreviews;
CacheSize: Integer;
private
FImages: TImageList;
FLoadingPoll: TThreadPool;
FPath: string;
FPreviewSize: TSizeF;
procedure SetImages(const Value: TImageList);
procedure Drop;
procedure SetPath(const Value: string);
procedure SetPreviewSize(const Value: TSizeF);
public
constructor Create;
destructor Destroy; override;
property Images: TImageList read FImages write SetImages;
property Path: string read FPath write SetPath;
property PreviewSize: TSizeF read FPreviewSize write SetPreviewSize;
class function GetInstance: TPreviews;
class function GetByURL(const URL: string): TPreview;
class function GetByName(const ImageName: string): TPreview;
class function GetPreview(const URL, ImageName: string; const DefaultIndex: Integer): TBitmap;
class function GetDefualt(const ImageIndex: Integer): TBitmap;
class procedure Reset;
end;
implementation
uses
System.IOUtils, System.Net.HttpClient;
{ TPreviews }
class procedure TPreviews.Reset;
var
FOldPool: TThreadPool;
begin
with GetInstance do
begin
if Assigned(FLoadingPoll) then
begin
FOldPool := FLoadingPoll;
TThread.CreateAnonymousThread(
procedure
begin
FOldPool.Free;
end).Start;
end;
FLoadingPoll := TThreadPool.Create;
end;
end;
constructor TPreviews.Create;
begin
inherited;
FPreviewSize := TSizeF.Create(256, 256);
end;
destructor TPreviews.Destroy;
begin
if Assigned(FLoadingPoll) then
FLoadingPoll.Free;
inherited;
end;
procedure TPreviews.Drop;
var
i, c: Integer;
begin
if Count > (CacheSize * 2) then
begin
i := 0;
c := 0;
while i < CacheSize do
begin
Inc(i);
if not Items[c].Loading then
Delete(c)
else
Inc(c);
end;
end;
end;
class function TPreviews.GetByName(const ImageName: string): TPreview;
var
Item: TPreview;
begin
Result := nil;
for Item in GetInstance do
if Item.ImageName = ImageName then
Exit(Item);
end;
class function TPreviews.GetByURL(const URL: string): TPreview;
var
Item: TPreview;
begin
Result := nil;
for Item in GetInstance do
if Item.URL = URL then
Exit(Item);
end;
class function TPreviews.GetDefualt(const ImageIndex: Integer): TBitmap;
begin
Result := nil;
with GetInstance do
begin
if Assigned(Images) then
begin
Result := Images.Bitmap(FPreviewSize, ImageIndex);
end;
end;
end;
class function TPreviews.GetInstance: TPreviews;
begin
if not Assigned(Instance) then
begin
Instance := TPreviews.Create;
Instance.CacheSize := 100;
end;
Result := Instance;
end;
class function TPreviews.GetPreview(const URL, ImageName: string; const DefaultIndex: Integer): TBitmap;
var
Item: TPreview;
FN: string;
begin
with GetInstance do
begin
Drop;
//Find to cache
Item := GetByURL(URL);
if Assigned(Item) then
Exit(Item)
else
begin
//Find to cache
Item := GetByName(ImageName);
if Assigned(Item) then
Exit(Item)
else
Item := TPreview.Create;
end;
//Find and load from FS
FN := TPath.Combine(FPath, ImageName);
if TFile.Exists(FN) then
begin
try
Item.LoadThumbnailFromFile(FN, FPreviewSize.Width, FPreviewSize.Height);
Item.URL := URL;
Item.ImageName := ImageName;
Add(Item);
Exit(Item);
except
TFile.Delete(FN);
end;
end;
//Load from server
Item.Assign(GetDefualt(DefaultIndex));
Item.URL := URL;
Item.Loading := True;
Add(Item);
Result := Item;
//Loading
TTask.Run(
procedure
var
HTTP: THTTPClient;
Mem: TMemoryStream;
begin
HTTP := THTTPClient.Create;
try
Mem := TMemoryStream.Create;
try
if HTTP.Get(URL, Mem).StatusCode = 200 then
begin
Mem.Position := 0;
TThread.Synchronize(nil,
procedure
begin
Item.LoadFromStream(Mem);
end);
if TDirectory.Exists(FPath) then
begin
try
TFile.Create(FN).Free;
Mem.SaveToFile(FN);
except
if TFile.Exists(FN) then
TFile.Delete(FN);
end;
end;
end;
finally
Mem.Free;
end;
except
if TFile.Exists(FN) then
TFile.Delete(FN);
end;
Item.Loading := False;
HTTP.Free;
end, FLoadingPoll);
end;
end;
procedure TPreviews.SetImages(const Value: TImageList);
begin
FImages := Value;
end;
procedure TPreviews.SetPath(const Value: string);
begin
FPath := Value;
end;
procedure TPreviews.SetPreviewSize(const Value: TSizeF);
begin
FPreviewSize := Value;
end;
initialization
finalization
TPreviews.GetInstance.Free;
end.