ImageCache large images
Summary
#The maxByteSize
of the ImageCache
is no longer
automatically made larger to accommodate large images.
Context
#Previously, when loading images into the ImageCache
that had larger byte sizes than the ImageCache
's maxByteSize
,
Flutter permanently increased the maxByteSize
value
to accommodate those images.
This logic sometimes led to bloated maxByteSize
values that
made working in memory-limited systems more difficult.
Description of change
#The following "before" and "after" pseudocode demonstrates
the changes made to the ImageCache
algorithm:
// Old logic pseudocode
void onLoadImage(Image image) {
if (image.byteSize > _cache.maxByteSize) {
_cache.maxByteSize = image.byteSize + 1000;
}
_cache.add(image);
while (_cache.count > _cache.maxCount
|| _cache.byteSize > _cache.maxByteSize) {
_cache.discardOldestImage();
}
}
// New logic pseudocode
void onLoadImage(Image image) {
if (image.byteSize < _cache.maxByteSize) {
_cache.add(image);
while (_cache.count > _cache.maxCount
|| _cache.byteSize > cache.maxByteSize) {
cache.discardOldestImage();
}
}
}
Migration guide
#There might be situations where the ImageCache
is thrashing with the new logic where it wasn't previously,
specifically if you load images that are larger than your
cache.maxByteSize
value.
This can be remedied by one of the following approaches:
- Increase the
ImageCache.maxByteSize
value to accommodate larger images. - Adjust your image loading logic to guarantee that
the images fit nicely into the
ImageCache.maxByteSize
value of your choosing. - Subclass
ImageCache
, implement your desired logic, and create a new binding that serves up your subclass ofImageCache
(see theimage_cache.dart
source).
Timeline
#The old algorithm is no longer supported.
Landed in version: 1.16.3
In stable release: 1.17
References
#API documentation:
Relevant issue:
Relevant PR:
Other:
除非另有说明,本文档之所提及适用于 Flutter 的最新稳定版本,本页面最后更新时间: 2024-04-04。 查看文档源码 或者 为本页面内容提出建议。