Skip to content

Commit 5f47563

Browse files
committed
Step 14.16: Implement getPicture method in picture service
1 parent ea9cd55 commit 5f47563

File tree

1 file changed

+30
-14
lines changed

1 file changed

+30
-14
lines changed

src/services/picture.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,50 @@
11
import { Injectable } from '@angular/core';
22
import { Platform } from 'ionic-angular';
3-
import { ImagePicker } from '@ionic-native/image-picker';
43
import { UploadFS } from 'meteor/jalik:ufs';
54
import { PicturesStore } from 'api/collections';
65
import * as _ from 'lodash';
76
import { DEFAULT_PICTURE_URL } from 'api/models';
7+
import { Camera, CameraOptions } from '@ionic-native/camera';
8+
import { Crop } from '@ionic-native/crop';
89

910
@Injectable()
1011
export class PictureService {
1112
constructor(private platform: Platform,
12-
private imagePicker: ImagePicker) {
13+
private camera: Camera,
14+
private crop: Crop) {
1315
}
1416

15-
select(): Promise<File> {
16-
if (!this.platform.is('cordova') || !this.platform.is('mobile')) {
17+
getPicture(camera: boolean, crop: boolean): Promise<File> {
18+
if (!this.platform.is('cordova')) {
1719
return new Promise((resolve, reject) => {
18-
try {
19-
UploadFS.selectFile((file: File) => {
20-
resolve(file);
21-
});
22-
}
23-
catch (e) {
24-
reject(e);
20+
//TODO: add javascript image crop
21+
if (camera === true) {
22+
reject(new Error("Can't access the camera on Browser"));
23+
} else {
24+
try {
25+
UploadFS.selectFile((file: File) => {
26+
resolve(file);
27+
});
28+
} catch (e) {
29+
reject(e);
30+
}
2531
}
2632
});
2733
}
2834

29-
return this.imagePicker.getPictures({maximumImagesCount: 1}).then((URL: string) => {
30-
return this.convertURLtoBlob(URL);
31-
});
35+
return this.camera.getPicture(<CameraOptions>{
36+
destinationType: 1,
37+
quality: 50,
38+
correctOrientation: true,
39+
saveToPhotoAlbum: false,
40+
sourceType: camera ? 1 : 0
41+
})
42+
.then((fileURI) => {
43+
return crop ? this.crop.crop(fileURI, {quality: 50}) : fileURI;
44+
})
45+
.then((croppedFileURI) => {
46+
return this.convertURLtoBlob(croppedFileURI);
47+
});
3248
}
3349

3450
upload(blob: File): Promise<any> {

0 commit comments

Comments
 (0)