11import { Injectable } from '@angular/core' ;
2- import { Plugins , CameraResultType , Capacitor , FilesystemDirectory } from '@capacitor/core' ;
2+ import { Plugins , CameraResultType , Capacitor , FilesystemDirectory , CameraPhoto , CameraSource } from '@capacitor/core' ;
33
44const { Camera, Filesystem, Storage } = Plugins ;
55
@@ -13,47 +13,59 @@ export class PhotoService {
1313 constructor ( ) { }
1414
1515 async loadSaved ( ) {
16+ // Retrieve cached photo array data
1617 const photos = await Storage . get ( { key : this . PHOTO_STORAGE } ) ;
1718 this . photos = JSON . parse ( photos . value ) || [ ] ;
1819 }
1920
20- // Use the device camera to take a photo.
21- // Store the photo data into permanent file storage.
22- // Store a reference to the photo in an array.
23- async takePhoto ( ) {
21+ /* Use the device camera to take a photo:
22+ // https://capacitor.ionicframework.com/docs/apis/camera
23+
24+ // Store the photo data into permanent file storage:
25+ // https://capacitor.ionicframework.com/docs/apis/filesystem
26+
27+ // Store a reference to all photo filepaths using Storage API:
28+ // https://capacitor.ionicframework.com/docs/apis/storage
29+ */
30+ public async takePhoto ( ) {
2431 // Take a photo
2532 const capturedPhoto = await Camera . getPhoto ( {
26- resultType : CameraResultType . Uri
33+ resultType : CameraResultType . Uri , // file-based data; provides best performance
34+ source : CameraSource . Camera , // automatically take a new photo with the camera
35+ quality : 100 // highest quality (0 to 100)
2736 } ) ;
2837
2938 const savedImageFile = await this . savePicture ( capturedPhoto ) ;
3039
40+ // Add new photo to Photos array
3141 this . photos . unshift ( {
3242 filepath : savedImageFile ,
3343 webviewPath : Capacitor . convertFileSrc ( savedImageFile )
3444 } ) ;
3545
3646 // Cache all photo data for future retrieval
37- //this.storage.set(this.PHOTO_STORAGE, this.photos);
47+ Storage . set ( {
48+ key : this . PHOTO_STORAGE ,
49+ value : JSON . stringify ( this . photos )
50+ } ) ;
3851 }
3952
4053 // Save picture to file on device
41- async savePicture ( cameraImage ) {
54+ private async savePicture ( cameraPhoto : CameraPhoto ) {
4255 // Read the file into its base64 version
4356 const readFile = await Filesystem . readFile ( {
44- path : cameraImage
57+ path : cameraPhoto . path
4558 } ) ;
4659
4760 // Write the file to the data directory (instead of temp storage)
4861 const fileName = new Date ( ) . getTime ( ) + '.jpeg' ;
49-
5062 await Filesystem . writeFile ( {
5163 path : fileName ,
5264 data : readFile . data ,
5365 directory : FilesystemDirectory . Data
5466 } ) ;
5567
56- // Get the full filepath
68+ // Get the new, complete filepath
5769 const fileUri = await Filesystem . getUri ( {
5870 directory : FilesystemDirectory . Data ,
5971 path : fileName
@@ -63,14 +75,18 @@ export class PhotoService {
6375 }
6476
6577 // Delete picture by removing it from reference data and the filesystem
66- async deletePicture ( photo , position ) {
67- // Remove from our Photos reference data array
78+ public async deletePicture ( photo : Photo , position : number ) {
79+ // Remove this photo from the Photos reference data array
6880 this . photos . splice ( position , 1 ) ;
69- //await this.storage.set(PHOTO_STORAGE, this.photos);
7081
71- // delete photo file
72- const filename = photo . filepath . substr ( photo . filepath . lastIndexOf ( '/' ) + 1 ) ;
82+ // Update photos array cache by overwriting the existing photo array
83+ Storage . set ( {
84+ key : this . PHOTO_STORAGE ,
85+ value : JSON . stringify ( this . photos )
86+ } ) ;
7387
88+ // delete photo file from filesystem
89+ const filename = photo . filepath . substr ( photo . filepath . lastIndexOf ( '/' ) + 1 ) ;
7490 await Filesystem . deleteFile ( {
7591 path : filename ,
7692 directory : FilesystemDirectory . Data
0 commit comments