mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-11-05 13:26:48 +08:00
fix: memory leaks around image picking/saving to device
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
<Button text="box-shadow" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
|
||||
<Button text="css-playground" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
|
||||
<Button text="image-async" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
|
||||
<Button text="image-handling" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
|
||||
<Button text="list-page" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
|
||||
<Button text="root-layout" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
|
||||
<Button text="switch" tap="{{ viewDemo }}" class="btn btn-primary btn-view-demo" />
|
||||
|
||||
54
apps/toolbox/src/pages/image-handling.ts
Normal file
54
apps/toolbox/src/pages/image-handling.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { Observable, EventData, Page, ImageSource, knownFolders, path } from '@nativescript/core';
|
||||
import { create, ImagePickerMediaType } from '@nativescript/imagepicker';
|
||||
|
||||
let page: Page;
|
||||
|
||||
export function navigatingTo(args: EventData) {
|
||||
page = <Page>args.object;
|
||||
page.bindingContext = new DemoModel();
|
||||
}
|
||||
|
||||
export class DemoModel extends Observable {
|
||||
addingPhoto = false;
|
||||
|
||||
pickImage() {
|
||||
const context = create({
|
||||
mode: 'single',
|
||||
mediaType: ImagePickerMediaType.Image,
|
||||
});
|
||||
|
||||
context
|
||||
.authorize()
|
||||
.then(() => {
|
||||
return context.present();
|
||||
})
|
||||
.then((selection) => {
|
||||
const imageAsset = selection.length > 0 ? selection[0] : null;
|
||||
if (imageAsset) {
|
||||
this.addingPhoto = true;
|
||||
|
||||
ImageSource.fromAsset(imageAsset).then(
|
||||
(savedImage) => {
|
||||
const folder = knownFolders.documents();
|
||||
const filePath = path.join(folder.path, `assets-${Date.now()}.jpg`);
|
||||
console.log('filePath:', filePath);
|
||||
|
||||
const saved = savedImage.saveToFile(filePath, 'jpg');
|
||||
if (saved) {
|
||||
console.log(`file saved:`, filePath);
|
||||
} else {
|
||||
console.log('file not saved!');
|
||||
}
|
||||
this.addingPhoto = false;
|
||||
},
|
||||
(err) => {
|
||||
this.addingPhoto = false;
|
||||
}
|
||||
);
|
||||
}
|
||||
})
|
||||
.catch((e) => {
|
||||
console.log(e);
|
||||
});
|
||||
}
|
||||
}
|
||||
13
apps/toolbox/src/pages/image-handling.xml
Normal file
13
apps/toolbox/src/pages/image-handling.xml
Normal file
@@ -0,0 +1,13 @@
|
||||
<Page xmlns="http://schemas.nativescript.org/tns.xsd" navigatingTo="navigatingTo" class="page">
|
||||
<Page.actionBar>
|
||||
<ActionBar title="Permissions" icon="" class="action-bar">
|
||||
</ActionBar>
|
||||
</Page.actionBar>
|
||||
|
||||
<StackLayout class="p-20">
|
||||
<Label text="Test Memory leaks with image picking and saving to device. Best to profile from platform IDE like Xcode." textWrap="true" />
|
||||
|
||||
<Button text="Pick and Save Image" tap="{{ pickImage }}" />
|
||||
|
||||
</StackLayout>
|
||||
</Page>
|
||||
Reference in New Issue
Block a user