Skip to content

Commit c068eeb

Browse files
committed
⚡️ Improve image type determine when resolving image data
1 parent 973fdcb commit c068eeb

File tree

1 file changed

+42
-24
lines changed

1 file changed

+42
-24
lines changed

lib/src/provider/asset_entity_image_provider.dart

Lines changed: 42 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -70,37 +70,54 @@ class AssetEntityImageProvider extends ImageProvider<AssetEntityImageProvider> {
7070
AssetEntityImageProvider key,
7171
DecoderCallback decode,
7272
) async {
73-
assert(key == this);
74-
Uint8List? data;
75-
if (isOriginal) {
73+
try {
74+
assert(key == this);
7675
if (key.entity.type == AssetType.audio ||
7776
key.entity.type == AssetType.other) {
7877
throw UnsupportedError(
7978
'Image data for the ${key.entity.type} is not supported.',
8079
);
8180
}
82-
if (key.entity.type == AssetType.video) {
81+
Uint8List? data;
82+
final ImageFileType _type;
83+
if (key.imageFileType == ImageFileType.other) {
84+
// Assume the title is invalid here, try again with the async getter.
85+
_type = _getType(await key.entity.titleAsync);
86+
} else {
87+
_type = key.imageFileType;
88+
}
89+
if (isOriginal) {
90+
if (key.entity.type == AssetType.video) {
91+
data = await key.entity.thumbDataWithOption(
92+
_thumbOption(
93+
Constants.defaultGridThumbSize,
94+
Constants.defaultGridThumbSize,
95+
),
96+
);
97+
} else if (_type == ImageFileType.heic) {
98+
data = await (await key.entity.file)?.readAsBytes();
99+
} else {
100+
data = await key.entity.originBytes;
101+
}
102+
} else {
103+
final List<int> _thumbSize = thumbSize!;
83104
data = await key.entity.thumbDataWithOption(
84-
_thumbOption(
85-
Constants.defaultGridThumbSize,
86-
Constants.defaultGridThumbSize,
87-
),
105+
_thumbOption(_thumbSize[0], _thumbSize[1]),
88106
);
89-
} else if (imageFileType == ImageFileType.heic) {
90-
data = await (await key.entity.file)?.readAsBytes();
91-
} else {
92-
data = await key.entity.originBytes;
93107
}
94-
} else {
95-
final List<int> _thumbSize = thumbSize!;
96-
data = await key.entity.thumbDataWithOption(
97-
_thumbOption(_thumbSize[0], _thumbSize[1]),
98-
);
99-
}
100-
if (data == null) {
101-
throw AssertionError('Null data in entity: $entity');
108+
if (data == null) {
109+
throw StateError('The data of the entity is null: $entity');
110+
}
111+
return decode(data);
112+
} catch (e) {
113+
// Depending on where the exception was thrown, the image cache may not
114+
// have had a chance to track the key in the cache at all.
115+
// Schedule a microtask to give the cache a chance to add the key.
116+
Future<void>.microtask(() {
117+
PaintingBinding.instance?.imageCache?.evict(key);
118+
});
119+
rethrow;
102120
}
103-
return decode(data);
104121
}
105122

106123
ThumbOption _thumbOption(int width, int height) {
@@ -120,10 +137,11 @@ class AssetEntityImageProvider extends ImageProvider<AssetEntityImageProvider> {
120137
/// ⚠ Not all the system version support read file name from the entity,
121138
/// so this method might not work sometime.
122139
/// 并非所有的系统版本都支持读取文件名,所以该方法有时无法返回正确的type。
123-
ImageFileType _getType() {
140+
ImageFileType _getType([String? filename]) {
124141
ImageFileType? type;
125-
final String? extension =
126-
entity.mimeType?.split('/').last ?? entity.title?.split('.').last;
142+
final String? extension = filename?.split('.').last ??
143+
entity.mimeType?.split('/').last ??
144+
entity.title?.split('.').last;
127145
if (extension != null) {
128146
switch (extension.toLowerCase()) {
129147
case 'jpg':

0 commit comments

Comments
 (0)