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
@@ -45,33 +52,53 @@ Our `usePhotoGallery` function exposes a method called takePhoto, which in turn
45
52
46
53
Notice the magic here: there's no platform-specific code (web, iOS, or Android)! The Capacitor Camera plugin abstracts that away for us, leaving just one method call - `getPhoto()` - that will open up the device's camera and allow us to take photos.
47
54
48
-
The last step we need to take is to use the new function from the Tab2 page. Go back to `Tab2Page.vue` and import it:
// CHANGE: Destructure `takePhoto` from `usePhotoGallery().
75
102
const { takePhoto } =usePhotoGallery();
76
103
</script>
77
104
```
@@ -86,68 +113,190 @@ After taking a photo, it disappears right away. We still need to display it with
86
113
87
114
## Displaying Photos
88
115
89
-
First we will create a new type to define our Photo, which will hold specific metadata. Add the following UserPhoto interface to the `usePhotoGallery.ts` file, somewhere outside of the main function:
116
+
First we will create a new type to define our Photo, which will hold specific metadata. Back in `usePhotoGallery.ts`, add the following `UserPhoto` interface below the main function:
90
117
91
-
```tsx
118
+
```typescript
119
+
exportconst usePhotoGallery = () => {
120
+
// Same old code from before.
121
+
};
122
+
123
+
// CHANGE: Add the `UserPhoto` interface.
92
124
exportinterfaceUserPhoto {
93
125
filepath:string;
94
126
webviewPath?:string;
95
127
}
96
128
```
97
129
98
-
At the top of the `usePhotoGallery` function, define an array so we can store each photo captured with the Camera. Make it a reactive variable using Vue's [ref function](https://v3.vuejs.org/guide/composition-api-introduction.html#reactive-variables-with-ref).
130
+
At the top of the `usePhotoGallery` function, define an array so we can store each photo captured with the Camera. Make it a reactive variable using Vue's [ref function](https://vuejs.org/api/reactivity-core.html#ref).
99
131
100
-
```tsx
101
-
const photos =ref<UserPhoto[]>([]);
132
+
```typescript
133
+
exportconst usePhotoGallery = () => {
134
+
// CHANGE: Add the `photos` array.
135
+
const photos =ref<UserPhoto[]>([]);
136
+
137
+
// other code
138
+
};
102
139
```
103
140
104
-
When the camera is done taking a picture, the resulting `Photo` returned from Capacitor will be added to the `photos` array. Update the `takePhoto` function, adding this code after the `Camera.getPhoto` line:
141
+
When the camera is done taking a picture, the resulting `Photo` returned from Capacitor will be added to the `photos` array. Update the `takePhoto` function with the following:
105
142
106
-
```tsx
107
-
const fileName =Date.now() +'.jpeg';
108
-
const savedFileImage = {
109
-
filepath: fileName,
110
-
webviewPath: photo.webPath,
111
-
};
143
+
```typescript
144
+
const takePhoto =async () => {
145
+
const photo =awaitCamera.getPhoto({
146
+
resultType: CameraResultType.Uri,
147
+
source: CameraSource.Camera,
148
+
quality: 100,
149
+
});
150
+
// CHANGE: Create the `fileName` with current timestamp.
// CHANGE: Add `photos` array to destructure from `usePhotoGallery()`.
134
242
const { photos, takePhoto } =usePhotoGallery();
243
+
</script>
135
244
```
136
245
137
246
With the photo(s) stored into the main array we can now display the images on the screen. Add a [Grid component](https://ionicframework.com/docs/api/grid) so that each photo will display nicely as they are added to the gallery, and loop through each photo in the Photos array, adding an Image component (`<ion-img>`) for each. Point the `src` (source) to the photo's path:
138
247
139
-
```tsx
140
-
<ion-content>
141
-
<ion-grid>
142
-
<ion-row>
143
-
<ion-colsize="6":key="photo.filepath"v-for="photo in photos">
144
-
<ion-img:src="photo.webviewPath"></ion-img>
145
-
</ion-col>
146
-
</ion-row>
147
-
</ion-grid>
148
-
149
-
<!--<ion-fab>markup-->
150
-
</ion-content>
248
+
```html
249
+
<template>
250
+
<ion-page>
251
+
<ion-header>
252
+
<ion-toolbar>
253
+
<ion-title>Photo Gallery</ion-title>
254
+
</ion-toolbar>
255
+
</ion-header>
256
+
<ion-content:fullscreen="true">
257
+
<ion-headercollapse="condense">
258
+
<ion-toolbar>
259
+
<ion-titlesize="large">Photo Gallery</ion-title>
260
+
</ion-toolbar>
261
+
</ion-header>
262
+
<!-- CHANGE: Add a grid component to display the photos. -->
263
+
<ion-grid>
264
+
<ion-row>
265
+
<!-- CHANGE: Create a new column and image component for each photo. -->
266
+
<ion-colsize="6":key="photo.filepath"v-for="photo in photos">
0 commit comments