|
| 1 | +import { Component, inject, TemplateRef, ViewChild, ViewContainerRef } from '@angular/core'; |
| 2 | +import { DemoSharedNativescriptViewShot } from '@demo/shared'; |
| 3 | +import { ImageSource, Screen, View } from '@nativescript/core'; |
| 4 | +import { renderToImageSource } from '@valor/nativescript-view-shot'; |
| 5 | +import { ViewShotService } from '@valor/nativescript-view-shot/angular'; |
| 6 | + |
| 7 | +@Component({ |
| 8 | + selector: 'demo-nativescript-view-shot', |
| 9 | + templateUrl: 'nativescript-view-shot.component.html', |
| 10 | +}) |
| 11 | +export class NativescriptViewShotComponent { |
| 12 | + demoShared: DemoSharedNativescriptViewShot; |
| 13 | + |
| 14 | + viewShotService = inject(ViewShotService); |
| 15 | + @ViewChild('viewHost', { read: ViewContainerRef }) vcRef: ViewContainerRef; |
| 16 | + srcUrl = `https://picsum.photos/${Screen.mainScreen.widthPixels}/200`; |
| 17 | + webImage$ = ImageSource.fromUrl(this.srcUrl); |
| 18 | + webImage2: ImageSource; |
| 19 | + imgSrc: ImageSource; |
| 20 | + loading = false; |
| 21 | + targetWidth = `${Screen.mainScreen.widthDIPs}`; |
| 22 | + |
| 23 | + async renderTemplate(template: TemplateRef<unknown>, attached: boolean) { |
| 24 | + let width = parseInt(this.targetWidth, 10); |
| 25 | + if (isNaN(width)) { |
| 26 | + width = Screen.mainScreen.widthDIPs; |
| 27 | + } |
| 28 | + this.loading = true; |
| 29 | + // reusing imageSource on iOS sometimes doesn't work, so we create a new one each time |
| 30 | + this.webImage2 = new ImageSource((await this.webImage$).ios); |
| 31 | + this.loading = false; |
| 32 | + this.imgSrc = await this.viewShotService.captureInBackground(template, { |
| 33 | + logicalHost: attached ? this.vcRef : undefined, |
| 34 | + width, |
| 35 | + delay: 0, |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + screenshotView(view: View) { |
| 40 | + this.imgSrc = renderToImageSource(view); |
| 41 | + } |
| 42 | +} |
0 commit comments