Skip to content

Commit c139a4a

Browse files
Dimitar TachevDimitar Tachev
authored andcommitted
Improve the demos ensuring permissions requested before taking a picture.
1 parent c82d87d commit c139a4a

File tree

5 files changed

+49
-59
lines changed

5 files changed

+49
-59
lines changed

demo-angular/app/app.component.html

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,5 @@
44
<Switch [(ngModel)]="saveToGallery"></Switch>
55
</StackLayout>
66
<Image row="1" [src]="cameraImage" stretch="fill" margin="10"></Image>
7-
<StackLayout orientation="horizontal" row="2" padding="10">
8-
<Button text="Take Picture" (tap)='onTakePictureTap($event)'></Button>
9-
<Button text="Request Permissions" (tap)='onRequestPermissionsTap()'></Button>
10-
</StackLayout>
7+
<Button text="Take Picture" (tap)='onTakePictureTap($event)' row="2" padding="10"></Button>
118
</GridLayout>

demo-angular/app/app.component.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,30 @@ export class AppComponent {
1414
public cameraImage: ImageAsset;
1515

1616
onTakePictureTap(args) {
17-
takePicture({ width: 180, height: 180, keepAspectRatio: true, saveToGallery: this.saveToGallery })
18-
.then((imageAsset: any) => {
19-
this.cameraImage = imageAsset;
20-
21-
// if you need image source
22-
let source = new ImageSource();
23-
source.fromAsset(imageAsset).then((source) => {
24-
let width = source.width;
25-
let height = source.height;
26-
if (app.android) {
27-
// the android dimensions are in device pixels
28-
width = layout.toDeviceIndependentPixels(width);
29-
height = layout.toDeviceIndependentPixels(height);
30-
}
31-
32-
console.log(`Size: ${width}x${height}`);
33-
});
34-
}, (error) => {
35-
console.log("Error: " + error);
36-
});
37-
}
38-
39-
onRequestPermissionsTap() {
4017
requestPermissions().then(
41-
() => console.log('got permissions'),
42-
() => console.log('permissions rejected')
18+
() => {
19+
takePicture({ width: 180, height: 180, keepAspectRatio: true, saveToGallery: this.saveToGallery })
20+
.then((imageAsset: any) => {
21+
this.cameraImage = imageAsset;
22+
23+
// if you need image source
24+
let source = new ImageSource();
25+
source.fromAsset(imageAsset).then((source) => {
26+
let width = source.width;
27+
let height = source.height;
28+
if (app.android) {
29+
// the android dimensions are in device pixels
30+
width = layout.toDeviceIndependentPixels(width);
31+
height = layout.toDeviceIndependentPixels(height);
32+
}
33+
34+
console.log(`Size: ${width}x${height}`);
35+
});
36+
}, (error) => {
37+
console.log("Error: " + error);
38+
});
39+
},
40+
() => alert('permissions rejected')
4341
);
4442
}
4543
}

demo/app/main-page.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,33 @@ export function navigatingTo(args: EventData) {
1818
page.bindingContext = fromObject({ cameraImage: picturePath, saveToGallery: true });
1919
}
2020

21-
export function onRequestPermissionsTap(args: EventData) {
22-
requestPermissions().then(
23-
() => console.log('got permissions'),
24-
() => console.log('permissions rejected')
25-
);
26-
}
27-
2821
export function onTakePictureTap(args: EventData) {
2922
let page = <Page>(<View>args.object).page;
3023
let saveToGallery = page.bindingContext.get("saveToGallery");
31-
takePicture({ width: 180, height: 180, keepAspectRatio: true, saveToGallery: saveToGallery }).
32-
then((imageAsset) => {
33-
page.bindingContext.set("cameraImage", imageAsset);
24+
requestPermissions().then(
25+
() => {
26+
takePicture({ width: 180, height: 180, keepAspectRatio: true, saveToGallery: saveToGallery }).
27+
then((imageAsset) => {
28+
page.bindingContext.set("cameraImage", imageAsset);
3429

35-
// if you need image source
36-
let source = new imageSourceModule.ImageSource();
37-
source.fromAsset(imageAsset).then((source) => {
38-
let width = source.width;
39-
let height = source.height;
40-
if (app.android) {
41-
// the android dimensions are in device pixels
42-
width = layout.toDeviceIndependentPixels(width);
43-
height = layout.toDeviceIndependentPixels(height);
44-
}
30+
// if you need image source
31+
let source = new imageSourceModule.ImageSource();
32+
source.fromAsset(imageAsset).then((source) => {
33+
let width = source.width;
34+
let height = source.height;
35+
if (app.android) {
36+
// the android dimensions are in device pixels
37+
width = layout.toDeviceIndependentPixels(width);
38+
height = layout.toDeviceIndependentPixels(height);
39+
}
4540

46-
console.log(`Size: ${width}x${height}`);
47-
});
41+
console.log(`Size: ${width}x${height}`);
42+
});
43+
},
44+
(err) => {
45+
console.log("Error -> " + err.message);
46+
});
4847
},
49-
(err) => {
50-
console.log("Error -> " + err.message);
51-
});
48+
() => alert('permissions rejected')
49+
);
5250
}

demo/app/main-page.xml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@
55
<Switch checked="{{ saveToGallery }}"/>
66
</StackLayout>
77
<Image row="1" src="{{ cameraImage }}" id="image" stretch="fill" margin="10"/>
8-
<StackLayout orientation="horizontal" row="2" padding="10">
9-
<Button text="Take Picture" tap="onTakePictureTap" />
10-
<Button text="Request Permissions" tap="onRequestPermissionsTap" />
11-
</StackLayout>
8+
<Button text="Take Picture" tap="onTakePictureTap" row="2" padding="10"/>
129
</GridLayout>
1310
</Page>

src/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nativescript-camera",
3-
"version": "3.4.0",
3+
"version": "3.2.0",
44
"description": "Provides API for using device camera",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)