Image 如何仅捕获renderObject中的图像,而不捕获Flatter中的周围空间?
图像是使用Image 如何仅捕获renderObject中的图像,而不捕获Flatter中的周围空间?,image,flutter,Image,Flutter,图像是使用CustomPaint创建的,它有一个方形画布,如下所示: _imagePaint = CustomPaint( size: Size(width, width), painter: imagePainter(image: image), ); Container( height: Device.screenHeight * 0.56, child: RepaintBoundary( key: imageAreaKey,
CustomPaint
创建的,它有一个方形画布,如下所示:
_imagePaint = CustomPaint(
size: Size(width, width),
painter: imagePainter(image: image),
);
Container(
height: Device.screenHeight * 0.56,
child: RepaintBoundary(
key: imageAreaKey,
child: imagePaint,
), // RepaintBoundary
), // Container
// Getting the render object that contains the image on screen
final RenderRepaintBoundary boundary = imageAreaKey.currentContext.findRenderObject();
// Capturing the image inside the render object
final image = await boundary.toImage(pixelRatio: pixelRatio);
// Converting the image into ByteData
final byteData = await image.toByteData(format: ui.ImageByteFormat.png);
return byteData.buffer.asUint8List();
我将图像放置在一个重新绘制边界
中,如下所示:
_imagePaint = CustomPaint(
size: Size(width, width),
painter: imagePainter(image: image),
);
Container(
height: Device.screenHeight * 0.56,
child: RepaintBoundary(
key: imageAreaKey,
child: imagePaint,
), // RepaintBoundary
), // Container
// Getting the render object that contains the image on screen
final RenderRepaintBoundary boundary = imageAreaKey.currentContext.findRenderObject();
// Capturing the image inside the render object
final image = await boundary.toImage(pixelRatio: pixelRatio);
// Converting the image into ByteData
final byteData = await image.toByteData(format: ui.ImageByteFormat.png);
return byteData.buffer.asUint8List();
然后我像这样捕捉图像:
_imagePaint = CustomPaint(
size: Size(width, width),
painter: imagePainter(image: image),
);
Container(
height: Device.screenHeight * 0.56,
child: RepaintBoundary(
key: imageAreaKey,
child: imagePaint,
), // RepaintBoundary
), // Container
// Getting the render object that contains the image on screen
final RenderRepaintBoundary boundary = imageAreaKey.currentContext.findRenderObject();
// Capturing the image inside the render object
final image = await boundary.toImage(pixelRatio: pixelRatio);
// Converting the image into ByteData
final byteData = await image.toByteData(format: ui.ImageByteFormat.png);
return byteData.buffer.asUint8List();
但是结果显示图像周围有一些透明的空间。当我转换成JPG格式,将透明空间变成黑色时,这一点特别明显
请看下面的图片。请注意图像顶部和底部的黑色空格。如何在第一次仅捕获图像而不捕获空格
如果不可能,那么尽管屏幕大小不同,我如何摆脱它呢
注意:原始图像是一个完美的正方形
您可以尝试使
容器的高度等于设备的宽度
Container(
height: Device.screenWidth,
child: RepaintBoundary(
key: imageAreaKey,
child: imagePaint,
), // RepaintBoundary
),
起初我以为重新绘制边界会捕获它的孩子。但很明显,它是在捕捉它父母的体型。我想知道为什么。你能帮我把这个问题也投上一票吗?谢谢;-)看起来这是因为容器根据其自身大小调整其子容器的大小。因此,repainboundary
被赋予其父容器的大小,因此它捕获了额外的空间,该空间也与图像一起位于容器内。