Skip to content

Commit 2df4c86

Browse files
committed
fix: simplify imagecache examples
1 parent 139860f commit 2df4c86

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

content/core/image-cache.md

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
title: ImageCache
33
contributors:
44
- Ombuweb
5+
- NathanWalker
56
---
67

7-
ImageCache allows you to cache an image from an HTTP request.
8+
ImageCache allows you to cache an image from an HTTP request. You can alternatively use a plugin that auto handles image caching like [`@triniwiz/nativescript-image-cache-it`](https://www.npmjs.com/package/@triniwiz/nativescript-image-cache-it).
89

910
## Using ImageCache
1011

@@ -13,21 +14,15 @@ For consistency, you should use a single ImageCache instance throughout your app
1314
```ts
1415
import { ImageCache } from '@nativescript/core'
1516

16-
export class HelloWorldModel extends Observable {
17-
imageCache = new ImageCache()
18-
19-
constructor() {
20-
super()
21-
}
22-
}
17+
const imageCache = new ImageCache()
2318
```
2419

2520
### Save an image to cache
2621

2722
To retrieve an image from a remote server and cache it, use either the [push](#push) or [enqueue](#enqueue) method. These methods cache the `android.graphics.Bitmap` (Android) or `UIImage` (iOS) instance.
2823

2924
```ts
30-
this.imageCache.enqueue({
25+
imageCache.enqueue({
3126
url: this.url,
3227
key: 'cat',
3328
completed(image: android.graphics.Bitmap | UIImage, key) {
@@ -48,7 +43,7 @@ To call the [push](#push) method instead, just replace `enqueue` with `push`
4843
To get an image from the cache, use the [get](#get) method.
4944

5045
```ts
51-
const cachedImage = this.imageCache.get('cat')
46+
const cachedImage = imageCache.get('cat')
5247
```
5348

5449
## API
@@ -84,7 +79,7 @@ self.on(ImageCache.downloadErrorEvent, (args: DownloadError) => {
8479
### placeholder
8580

8681
```ts
87-
this.imageCache.placeholder = imageSource
82+
imageCache.placeholder = imageSource
8883
```
8984

9085
The image to be used to notify for a pending download request - e.g. loading indicator.
@@ -94,7 +89,7 @@ The image to be used to notify for a pending download request - e.g. loading ind
9489
### maxRequests
9590

9691
```ts
97-
this.imageCache.number = 2
92+
imageCache.number = 2
9893
```
9994

10095
The maximum number of simultaneous download requests. Defaults to 5
@@ -119,7 +114,7 @@ Temporary disables download requests.
119114
### push
120115

121116
```ts
122-
this.imageCache.push(request)
117+
imageCache.push(request)
123118
```
124119

125120
Adds a new image to the **start** of the cache. `request` is an object with the following members:
@@ -134,7 +129,7 @@ Adds a new image to the **start** of the cache. `request` is an object with the
134129
### enqueue
135130

136131
```ts
137-
this.imageCache.enqueue(request)
132+
imageCache.enqueue(request)
138133
```
139134

140135
Adds a new image to the **end** of the cache. For more details about the `request` parameter, see the [push()](#push) method.
@@ -144,15 +139,15 @@ Adds a new image to the **end** of the cache. For more details about the `reques
144139
### get
145140

146141
```ts
147-
this.imageCache.get(key)
142+
imageCache.get(key)
148143
```
149144

150145
Gets the image with a given `key` from the cache, or undefined if it's not in the cache.
151146

152147
### remove
153148

154149
```ts
155-
this.imageCache.remove(imageKey)
150+
imageCache.remove(imageKey)
156151
```
157152

158153
Remove the specified image from the cache.
@@ -162,7 +157,7 @@ Remove the specified image from the cache.
162157
### clear
163158

164159
```ts
165-
this.imageCache.clear()
160+
imageCache.clear()
166161
```
167162

168163
Clears all the images from the cache.

0 commit comments

Comments
 (0)