[web] memCacheWidth/memCacheHeight are ignored in the default HtmlImage mode, so every image decodes at full size
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 42/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 領域
- frontend, performance
調査の方向性
Start in the web implementation of cached_network_image_web, at the ImageRenderMethodForWeb.HtmlImage branch of _load that calls _loadAsyncHtmlImage and ui_web.createImageCodecFromUrl. Compare it with the HttpGet branch, which passes maxWidth/maxHeight through to a decode callback, and with Flutter's NetworkImage, which invokes decode so ResizeImage applies. The fix must let a memCacheWidth/memCacheHeight request reach the decode step while keeping the low-memory HtmlImage behaviour; reproduce with debugInvertOversizedImages = true in a CanvasKit debug build before changing anything.
索引モデルが issue の本文から書いたものです。
説明
🐛 Bug Report
On web, memCacheWidth and memCacheHeight have no effect when imageRenderMethodForWeb is left at its default, ImageRenderMethodForWeb.HtmlImage. Every image is decoded at its full source size, no matter how small it is shown. For example, a 40 px avatar from a 1024x1024 upload is held as a 4 MB decoded image instead of about 25 KB at 80x80.
Where it happens
CachedNetworkImage passes memCacheWidth/memCacheHeight to OctoImage, which wraps the provider in ResizeImage. ResizeImage only works if the provider calls the decode callback it is given. In cached_network_image_web, the HtmlImage branch never does:
case platform.ImageRenderMethodForWeb.HtmlImage:
return _loadAsyncHtmlImage(url, chunkEvents).asStream();
_loadAsyncHtmlImage calls ui_web.createImageCodecFromUrl directly, so the requested size is dropped. maxWidth/maxHeight reach _load, but only the HttpGet branch uses them.
Why HttpGet isn't a good workaround
HttpGet does honor the size, but on web:
flutter_cache_manager3.5.0 usesMemoryCacheSystemwithNonStoringObjectProvider, so downloaded files are kept in an in-memory file system inside the tab. In the 170-photo test below, that cost roughly 100 MB more JS heap than HtmlImage.MultiImageStreamCompleterkeeps a single-frame image's codec until the completer itself is disposed. Flutter'sMultiFrameImageStreamCompleterdisposes it right after emitting the frame.
Impact we measured
Release build in Chrome, a chat with 170 photos at their upload sizes (mostly 1365x1024 and 1024x1365), scrolled to the top and back:
| Default (HtmlImage) | Flutter NetworkImage + ResizeImage |
|
|---|---|---|
| Decoded bytes per photo, 1x screen | 6.4 MB | 0.69 MB |
| Decoded bytes per photo, 2x screen | 6.4 MB | 2.1 MB |
Photos that fit in a 512 MB ImageCache |
about 79 | all 170 |
| Photos fetched again when scrolling back (2x) | 72 of 170 | 0 |
| Chrome GPU process after scrolling (2x) | about 1.1 GB | 0.6 to 0.77 GB |
Expected behavior
On web, memCacheWidth and memCacheHeight limit the decoded size the same way they do on iOS and Android, without having to switch to HttpGet.
Reproduction steps
- In a Flutter web app (CanvasKit, debug mode), set
debugInvertOversizedImages = true;inmain(). - Show a large image at a small size with the default render method:
CachedNetworkImage(
imageUrl: largeImageUrl, // any 2000x1500 JPEG served with CORS
width: 40,
height: 40,
fit: BoxFit.cover,
memCacheWidth: 80,
memCacheHeight: 80,
)
- The image is drawn inverted and Flutter reports that its decode size is far larger than its display size, because it was decoded at 2000x1500.
- With
imageRenderMethodForWeb: ImageRenderMethodForWeb.HttpGet, or on iOS and Android, the same widget decodes at 80x80 and is not flagged.
Workaround we use for now
On web we build the same OctoImage that CachedNetworkImage.build builds, but with Flutter's NetworkImage as the provider. It calls the decode callback, so OctoImage's ResizeImage applies, and nothing keeps the download after decoding. If that fetch fails, we fall back to the regular CachedNetworkImage. Trimmed version:
class WebSizedCachedNetworkImage extends CachedNetworkImage {
WebSizedCachedNetworkImage({super.key, required super.imageUrl, super.placeholder,
super.errorWidget, super.width, super.height, super.fit,
super.memCacheWidth, super.memCacheHeight});
@override
Widget build(BuildContext context) {
if (!kIsWeb) return super.build(context);
return OctoImage(
image: NetworkImage(imageUrl, webHtmlElementStrategy: WebHtmlElementStrategy.never),
memCacheWidth: memCacheWidth,
memCacheHeight: memCacheHeight,
width: width,
height: height,
fit: fit,
placeholderBuilder: (context) => placeholder?.call(context, imageUrl) ?? const SizedBox(),
errorBuilder: (context, error, stackTrace) => CachedNetworkImage(
imageUrl: imageUrl,
placeholder: placeholder,
errorWidget: errorWidget,
width: width,
height: height,
fit: fit,
memCacheWidth: memCacheWidth,
memCacheHeight: memCacheHeight,
),
);
}
}
A possible fix in the package: when memCacheWidth or memCacheHeight is set on web, load the bytes and call decode (as Flutter's NetworkImage does) instead of going through createImageCodecFromUrl, or make that the default web path.
Configuration
Version: cached_network_image 4.0.3, cached_network_image_web 2.0.2, flutter_cache_manager 3.5.0, octo_image 2.1.3 (Flutter 3.44.9, CanvasKit, Chrome). I also checked the source of 4.0.7 / web 2.0.5 and main: the HtmlImage default and branch are unchanged.
Platform:
- 📱 iOS
- 🤖 Android
- 🌐 Web
- 主要言語
- Dart
- スター
- 2.6k
- フォーク
- 742
- 平均マージ
- 1日 15時間
- マージ済み PR(30日)
- 4
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートあり
- コントリビューションガイドなし
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
Baseflow/flutter_cached_network_image のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
Baseflow/flutter_cached_network_image#492 · リアクション 2 件 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 68/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
-
難易度 2/5 1時間未満 初心者へのやさしさ 22/100
Baseflow/flutter_cached_network_image#1061 · コメント 1 件 ·
-
難易度 3/5 1〜2日 初心者へのやさしさ 55/100
Baseflow/flutter_cached_network_image の issue をすべて見る
似ている issue
-
BUG: App crashes on iOS when initializing speech-to-text due to missing microphone and speech recognition keys in Info.plist対応中かも @AyushSrivastava1818 が今日担当しました。 オープンbug enhancement
難易度 1/5 1時間未満 初心者へのやさしさ 78/100
AOSSIE-Org/Ell-ena#365 · コメント 2 件 ·
-
feature
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
VeryGoodOpenSource/vgv-ai-flutter-plugin#219 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 1/5 1時間未満 初心者へのやさしさ 72/100
cucumber/gherkin#766 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
Anxcye/anx-reader#1024 ·
-
[cloud_functions]: Apple platforms ignore custom domains passed as region対応中かも @ragavendranbala-TSL が今日担当しました。 オープンNeeds Attention platform: ios platform: macos plugin: functions type: bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
firebase/flutterfire#19015 · コメント 2 件 ·
メンテナーはふだん 4 日以内に返信