Skip to content

Commit

Permalink
feat: Prefetch image and return the base64/blob string
Browse files Browse the repository at this point in the history
  • Loading branch information
efstathiosntonas committed Aug 9, 2022
1 parent 2f23710 commit 47d537a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,24 @@ const urls = ["https://..../image.jpg", "https://..../image2.jpg", "https://....
CacheManager.prefetch(urls); // prefetch mutliple images
```

#### Prefetch Image(s) and return blob/base64:
Accepts 2 parameters:

| Parameter | Type | Description |
|-----------|----------|----------------------------------------------------------------------------------------|
| `image` | `String` | (Required) uri of remote image |
| `options` | `Object` | (Optional) custom options for the fetch image http request eg. `{headers:{}, body:{}}` |


```tsx
import { CacheManager } from '@georstat/react-native-image-cache';

const url = "https://..../image.jpg"

CacheManager.prefetchBlob(url).then(response => console.log(response)); // response is the base64 string

```

#### Clear local cache:

```tsx
Expand Down
12 changes: 12 additions & 0 deletions src/CacheManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,18 @@ export default class CacheManager {
}
}

static async prefetchBlob(
source: string,
options?: DownloadOptions
): Promise<string | undefined> {
const path = await CacheManager.get(source, options).getPath();
if (path) {
const blob = await FileSystem.readFile(path, 'base64');
return blob;
}
return undefined;
}

static async pruneCache() {
// If cache directory does not exist yet there's no need for pruning.
if (!(await CacheManager.getCacheSize())) {
Expand Down

0 comments on commit 47d537a

Please sign in to comment.