You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We’ve implemented photo taking and saving to the filesystem. There’s one last piece of functionality missing: the photos are stored in the filesystem, but we need a way to save pointers to each file so that they can be displayed again in the photo gallery.
8
9
9
-
Fortunately, this is easy: we’ll leverage the Capacitor [Preferences API](https://capacitorjs.com/docs/apis/preferences) to store our array of Photos in a key-value store.
10
+
Fortunately, this is easy: we’ll leverage the Capacitor [Preferences API](../../native/preferences.md) to store our array of Photos in a key-value store.
10
11
11
12
## Preferences API
12
13
13
14
Open `photo.service.ts` and begin by defining a new property in the `PhotoService` class that will act as the key for the store:
14
15
15
-
```tsx
16
+
```ts
16
17
exportclassPhotoService {
17
18
public photos:UserPhoto[] = [];
18
19
19
20
// CHANGE: Add a key for photo storage.
20
21
private PHOTO_STORAGE:string='photos';
21
22
22
-
constructor() {}
23
-
24
-
// other code...
23
+
// Same old code from before.
25
24
}
26
25
```
27
26
28
-
Next, at the end of the `addNewToGallery`function, add a call to `Preferences.set()` to save the Photos array. By adding it here, the Photos array is stored each time a new photo is taken. This way, it doesn’t matter when the app user closes or switches to a different app - all photo data is saved.
27
+
Next, at the end of the `addNewToGallery`method, add a call to `Preferences.set()` to save the `photos` array. By adding it here, the `photos` array is stored each time a new photo is taken. This way, it doesn’t matter when the app user closes or switches to a different app - all photo data is saved.
29
28
30
-
```tsx
29
+
```ts
31
30
publicasyncaddNewToGallery() {
31
+
// Take a photo
32
32
const capturedPhoto =awaitCamera.getPhoto({
33
33
resultType: CameraResultType.Uri,
34
34
source: CameraSource.Camera,
@@ -47,52 +47,51 @@ public async addNewToGallery() {
47
47
}
48
48
```
49
49
50
-
With the photo array data saved, create a new public method in the `PhotoService` class called `loadSaved()` that can retrieve the photo data. We use the same key to retrieve the photos array in JSON format, then parse it into an array:
51
-
52
-
```tsx
53
-
publicasyncaddNewToGallery() {
54
-
// Same old code from before.
55
-
}
50
+
With the photo array data saved, create a new public method in the `PhotoService` class called `loadSaved()` that can retrieve the photo data. We use the same key to retrieve the `photos` array in JSON format, then parse it into an array:
56
51
57
-
// CHANGE: Add the method to load the photo data.
58
-
publicasyncloadSaved() {
59
-
// Retrieve cached photo array data
60
-
const { value } =awaitPreferences.get({ key: this.PHOTO_STORAGE });
On mobile (coming up next!), we can directly set the source of an image tag - `<img src="x" />` - to each photo file on the Filesystem, displaying them automatically. On the web, however, we must read each image from the Filesystem into base64 format, using a new `base64` property on the `Photo` object. This is because the Filesystem API uses [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API) under the hood. Add the following code to complete the `loadSaved()`function:
65
+
On mobile (coming up next!), we can directly set the source of an image tag - `<img src="x" />` - to each photo file on the `Filesystem`, displaying them automatically. On the web, however, we must read each image from the `Filesystem` into base64 format, using a new `base64` property on the `Photo` object. This is because the `Filesystem` API uses [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API) under the hood. Add the following code to complete the `loadSaved()`method:
72
66
73
-
```tsx
74
-
publicasyncloadSaved() {
75
-
// Retrieve cached photo array data
76
-
const { value } =awaitPreferences.get({ key: this.PHOTO_STORAGE });
Our `PhotoService` can now load the saved images, but we'll need to update `tab2.page.ts` to put that new code to work. We'll call `loadSaved` within the [ngOnInit](https://angular.dev/guide/components/lifecycle#ngoninit) lifecycle method so that when the user first navigates to Tab 2 (the Photo Gallery), all photos are loaded and displayed on the screen.
178
+
Our `PhotoService` can now load the saved images, but we'll need to update `tab2.page.ts` to put that new code to work. We'll call `loadSaved` within the [ngOnInit](https://angular.dev/guide/components/lifecycle#ngoninit) lifecycle method so that when the user first navigates to the Photo Gallery, all photos are loaded and displayed on the screen.
@@ -223,4 +208,5 @@ If you're seeing broken image links or missing photos after following these step
223
208
224
209
In localStorage, look for domain `http://localhost:8100` and key `CapacitorStorage.photos`. In IndexedDB, find a store called "FileStorage". Your photos will have a key like `/DATA/123456789012.jpeg`.
225
210
:::
211
+
226
212
That’s it! We’ve built a complete Photo Gallery feature in our Ionic app that works on the web. Next up, we’ll transform it into a mobile app for iOS and Android!
0 commit comments