forked from NativeScript/NativeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage-cache.ios.ts
More file actions
101 lines (78 loc) · 3.06 KB
/
image-cache.ios.ts
File metadata and controls
101 lines (78 loc) · 3.06 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
import common = require("./image-cache-common");
import utils = require("utils/utils");
import trace = require("trace");
import * as httpRequestModule from "http/http-request";
var httpRequest: typeof httpRequestModule;
function ensureHttpRequest() {
if (!httpRequest) {
httpRequest = require("http/http-request");
}
}
//class NSCacheDelegateImpl extends NSObject implements NSCacheDelegate {
// public static ObjCProtocols = [NSCacheDelegate];
// static new(): NSCacheDelegateImpl {
// return <NSCacheDelegateImpl>super.new();
// }
// public cacheWillEvictObject(cache: NSCache, obj: any): void {
// trace.write("NSCacheDelegateImpl.cacheWillEvictObject(" + obj + ");", trace.categories.Debug);
// }
//}
class MemmoryWarningHandler extends NSObject {
static new(): MemmoryWarningHandler {
return <MemmoryWarningHandler>super.new();
}
private _cache: NSCache;
public initWithCache(cache: NSCache): MemmoryWarningHandler {
this._cache = cache;
NSNotificationCenter.defaultCenter().addObserverSelectorNameObject(this, "clearCache", "UIApplicationDidReceiveMemoryWarningNotification", null);
trace.write("[MemmoryWarningHandler] Added low memory observer.", trace.categories.Debug);
return this;
}
public dealloc(): void {
NSNotificationCenter.defaultCenter().removeObserverNameObject(this, "UIApplicationDidReceiveMemoryWarningNotification", null);
trace.write("[MemmoryWarningHandler] Removed low memory observer.", trace.categories.Debug);
super.dealloc();
}
public clearCache(): void {
trace.write("[MemmoryWarningHandler] Clearing Image Cache.", trace.categories.Debug);
this._cache.removeAllObjects();
utils.GC();
}
public static ObjCExposedMethods = {
"clearCache": { returns: interop.types.void, params: [] }
};
}
export class Cache extends common.Cache {
private _cache: NSCache;
//private _delegate: NSCacheDelegate;
private _memoryWarningHandler: MemmoryWarningHandler;
constructor() {
super();
this._cache = new NSCache();
//this._delegate = NSCacheDelegateImpl.new();
//this._cache.delegate = this._delegate;
this._memoryWarningHandler = MemmoryWarningHandler.new().initWithCache(this._cache);
}
public _downloadCore(request: common.DownloadRequest) {
ensureHttpRequest();
var that = this;
httpRequest.request({ url: request.url, method: "GET" })
.then(response => {
var image = UIImage.alloc().initWithData(response.content.raw);
that._onDownloadCompleted(request.key, image);
});
}
public get(key: string): any {
return this._cache.objectForKey(key);
}
public set(key: string, image: any): void {
this._cache.setObjectForKey(image, key);
}
public remove(key: string): void {
this._cache.removeObjectForKey(key);
}
public clear() {
this._cache.removeAllObjects();
utils.GC();
}
}