diff --git a/packages/flame/lib/extensions.dart b/packages/flame/lib/extensions.dart index cb3562c57e7..eb7061d9779 100644 --- a/packages/flame/lib/extensions.dart +++ b/packages/flame/lib/extensions.dart @@ -4,6 +4,7 @@ export 'src/extensions/image.dart'; export 'src/extensions/matrix4.dart'; export 'src/extensions/offset.dart'; export 'src/extensions/paint.dart'; +export 'src/extensions/picture_extension.dart'; export 'src/extensions/rect.dart'; export 'src/extensions/rectangle.dart'; export 'src/extensions/size.dart'; diff --git a/packages/flame/lib/src/components/text_box_component.dart b/packages/flame/lib/src/components/text_box_component.dart index 43808bbe6f0..3e371be947f 100644 --- a/packages/flame/lib/src/components/text_box_component.dart +++ b/packages/flame/lib/src/components/text_box_component.dart @@ -3,6 +3,7 @@ import 'dart:math' as math; import 'dart:ui'; import 'package:flame/components.dart'; +import 'package:flame/src/extensions/picture_extension.dart'; import 'package:flame/src/palette.dart'; import 'package:flame/src/text/text_renderer.dart'; import 'package:flutter/widgets.dart' hide Image; @@ -214,7 +215,7 @@ class TextBoxComponent extends TextComponent { final c = Canvas(recorder, size.toRect()); c.scale(pixelRatio); _fullRender(c); - return recorder.endRecording().toImage( + return recorder.endRecording().toImageSafe( (width * pixelRatio).ceil(), (height * pixelRatio).ceil(), ); diff --git a/packages/flame/lib/src/extensions/picture_extension.dart b/packages/flame/lib/src/extensions/picture_extension.dart new file mode 100644 index 00000000000..4056af08919 --- /dev/null +++ b/packages/flame/lib/src/extensions/picture_extension.dart @@ -0,0 +1,11 @@ +import 'dart:ui'; + +extension PictureExtension on Picture { + /// Converts [Picture] into an [Image] and disposes of the picture. + Future toImageSafe(int width, int height) { + return toImage(width, height).then((image) { + dispose(); + return image; + }); + } +} diff --git a/packages/flame/lib/src/image_composition.dart b/packages/flame/lib/src/image_composition.dart index 44a7cf9086e..32dae385a28 100644 --- a/packages/flame/lib/src/image_composition.dart +++ b/packages/flame/lib/src/image_composition.dart @@ -122,7 +122,7 @@ class ImageComposition { return recorder .endRecording() - .toImage(output.width.toInt(), output.height.toInt()); + .toImageSafe(output.width.toInt(), output.height.toInt()); } } diff --git a/packages/flame/lib/src/sprite_batch.dart b/packages/flame/lib/src/sprite_batch.dart index 18a49434c75..c26f9de5185 100644 --- a/packages/flame/lib/src/sprite_batch.dart +++ b/packages/flame/lib/src/sprite_batch.dart @@ -4,6 +4,7 @@ import 'dart:ui'; import 'package:flame/game.dart'; import 'package:flame/src/cache/images.dart'; import 'package:flame/src/extensions/image.dart'; +import 'package:flame/src/extensions/picture_extension.dart'; import 'package:flame/src/flame.dart'; extension SpriteBatchExtension on Game { @@ -191,7 +192,7 @@ class SpriteBatch { canvas.drawImage(image, Offset(-image.width * 2, 0), _emptyPaint); final picture = recorder.endRecording(); - final atlas = picture.toImage(image.width * 2, image.height); + final atlas = picture.toImageSafe(image.width * 2, image.height); return atlas; } diff --git a/packages/flame_tiled/test/tiled_test.dart b/packages/flame_tiled/test/tiled_test.dart index 85efe68bc0b..fa6b02faf0a 100644 --- a/packages/flame_tiled/test/tiled_test.dart +++ b/packages/flame_tiled/test/tiled_test.dart @@ -59,7 +59,7 @@ void main() { overlapMap.render(canvas); final picture = canvasRecorder.endRecording(); - final image = await picture.toImage(32, 16); + final image = await picture.toImageSafe(32, 16); final bytes = await image.toByteData(); canvasPixelData = bytes!.buffer.asUint8List(); }); @@ -144,7 +144,7 @@ void main() { overlapMap.render(canvas); final picture = canvasRecorder.endRecording(); - final image = await picture.toImage(64, 48); + final image = await picture.toImageSafe(64, 48); final bytes = await image.toByteData(); canvasPixelData = bytes!.buffer.asUint8List(); });