首页 > 解决方案 > 将视图转换为位图数据以另存为图像 | 本机脚本

问题描述

方法一:

这是我的ts 代码

 let makeImage = ViewModule.getViewById("makeImage"); 
  makeImage.android.setDrawingCacheEnabled(true);

在此处输入图像描述

无法使用“getViewById”获取视图。

查看代码:

<StackLayout id="makeImage">
                <Button text="Hi"></Button>
            </StackLayout>

方法二:

const view = application.android.foregroundActivity.getWindow().getDecorView().getRootView();
    view.setDrawingCacheEnabled(true);
    const bmp = android.graphics.Bitmap.createBitmap(view.getDrawingCache());
    let x = imageSourceModule.fromNativeSource(bmp);
    imageData = x.toBase64String("png");
    console.log(imageData);



 Result: Able to get the screenshot of whole page of parent. Not a mentioned section.

标签: androidnativescriptnativescript-angular

解决方案


你可以试试nativescript-screenshot插件,它当然在内部使用与 Android 相同的方法,并且也为 iOS 提供了等效方法。

始终使用该loaded事件来访问本机视图 ( .andorid/ .ios),以确保它已创建。

HTML

   <StackLayout (loaded)="onLoaded($event)">
      <Button text="Hi"></Button>
   </StackLayout>

TS

  onLoaded(event) {
      const imageSource = plugin.getImage(args.object);
  }

推荐阅读