-
Notifications
You must be signed in to change notification settings - Fork 60
/
asset.dart
37 lines (31 loc) 路 1018 Bytes
/
asset.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import 'dart:typed_data';
import 'dart:ui' as ui;
import 'package:butterfly_api/butterfly_api.dart';
import '../helpers/element_helper.dart';
class AssetService {
final NoteData document;
final Map<String, Uint8List> _cached = {};
final Map<String, ui.Image> _images = {};
AssetService(this.document);
Future<ui.Image?> getImage(String path) async {
if (_images.containsKey(path)) {
return _images[path]!.clone();
}
var listData = await getDataFromSource(document, path);
if (listData == null) {
listData = _cached[path];
if (listData == null) return null;
document.setAsset(Uri.parse(path).path, listData);
}
final data = Uint8List.fromList(listData);
_cached[path] = data;
final codec = await ui.instantiateImageCodec(data);
final frameInfo = await codec.getNextFrame();
final image = frameInfo.image;
_images[path] = image;
return image.clone();
}
void removeImage(String path) {
_images.remove(path)?.dispose();
}
}