【发布时间】:2019-05-10 05:13:06
【问题描述】:
我想知道是否可以捕获 webview 的图像?
这是我尝试过的:
例如使用 webview_flutter 0.1.0+1 :
static GlobalKey previewContainer = new GlobalKey();
...
new RaisedButton(
onPressed: takeScreenShot,
child: const Text('Take a Screenshot'),
),
...
takeScreenShot() async{
RenderRepaintBoundary boundary = previewContainer.currentContext.findRenderObject();
ui.Image image = await boundary.toImage();
final directory = (await getApplicationDocumentsDirectory()).path;
ByteData byteData = await image.toByteData(format: ui.ImageByteFormat.png);
Uint8List pngBytes = byteData.buffer.asUint8List();
File imgFile = new File(directory+'/screenshot.png');
imgFile.writeAsBytes(pngBytes);
//then, save the png image file in Firebase storage : OKAY
}
首先我尝试制作一个简单材质按钮的打印屏幕:好的,它可以工作
RepaintBoundary(
key: previewContainer,
child:
MaterialButton(
child: const Text('Test'),
textColor: Colors.white,
color: Colors.blue,
),
),
但是当我尝试制作 webview 的打印屏幕时,它不起作用,保存的图像是空的(只有几个字节):
RepaintBoundary(
key: previewContainer,
child:
new SizedBox(
height: 280.0,
width: 280.0,
child: WebView(
initialUrl: 'https://flutter.io',
javaScriptMode: JavaScriptMode.unrestricted,
onWebViewCreated: (WebViewController webViewController) {
_controller.complete(webViewController);
},
)
),
)
有什么想法吗?
或者除了 RepaintBoundary 还有其他方法可以捕获 png 图像吗?
【问题讨论】:
标签: javascript android webview dart flutter