Skip to content

Commit

Permalink
feat: Add support for base64 encoded images to be manually added to I…
Browse files Browse the repository at this point in the history
…mages cache. (#3008)

Adds a method so base64 encoded images can be manually added to the
Images cache.
  • Loading branch information
erickzanardo authored Jan 28, 2024
1 parent 45c87dd commit 1e56293
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/flame/lib/src/cache/images.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ class Images {
_assets[name] = _ImageAsset.fromImage(image);
}

/// Transform the base64 encoded image into an [Image] and adds it into the
/// cache.
Future<void> addFromBase64Data(String name, String base64Data) async {
_assets[name]?.dispose();
final image = await _fetchFromBase64(base64Data);
_assets[name] = _ImageAsset.fromImage(image);
}

/// If the image with [name] exists in the cache that is returned, otherwise
/// the image generated by [imageGenerator] is returned.
///
Expand Down
7 changes: 7 additions & 0 deletions packages/flame/test/cache/images_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ void main() {
'AAAAXNSR0IArs4c6QAAAA1JREFUGFdjWP33/n8ACGUDhwieHSEAAAAASUVORK5CYII=';

group('Images', () {
test('can add a base64 image to the cache', () async {
final cache = Images();
await cache.addFromBase64Data('img', pixel);

expect(cache.fromCache('img'), isA<Image>());
});

test('load image', () async {
final cache = Images();
final image = await cache.fromBase64('img', pixel);
Expand Down

0 comments on commit 1e56293

Please sign in to comment.