搜尋套件、儲存庫與 APP 分類

套件詳情v1.2.1

disposable_cached_images

一個用於從記憶體中顯示和釋放影像的 Flutter 套件。

近 30 天下載
155
喜歡數
36
Pub 分數
60

安裝套件

flutter pub add disposable_cached_images

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

一個專為高效顯示圖像而設計的Flutter套件,透過在不再需要時自動管理記憶體來釋放圖像。它透過暫時快取圖像並在使用後迅速釋放,優化效能,減少圖像密集型應用中的記憶體開銷。非常適合動態載入大量圖像的應用程式,例如相簿或社群媒體動態流。該套件可與Flutter的影像載入流程無縫整合,支援本機與網路影像。有助於防止記憶體洩漏並提升應用程式的回應速度。

適用情境

  • 圖像畫廊
  • 社群媒體訊息
  • 動態圖像顯示
  • 記憶體效率高的影像載入
  • 即時照片預覽

主要特色

  • 自動記憶體釋放
  • 臨時圖像快取
  • 支援網路和本機圖片
  • 防止記憶體洩漏
  • 與 Flutter 的無縫整合

適合對象

  • 高圖像密度的應用程式
  • 性能關鍵的UI
  • 記憶體受限環境
  • 開發人員優先考慮資源管理
  • 需要快速處理圖像的應用程式

使用建議

  • 僅限基本快取需求
  • 沒有高級快取淘汰策略
  • 不適用於持久圖像存儲
  • 在複雜情況下需要手動處理
  • 無內建影像壓縮

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

A flutter package for downloading, caching, displaying and releasing images from memory.

Features

Display images from the Internet and/or local files (assets and device storage).

Caching images in the cache directory.

Cancel the download if the image widget has been disposed to reduce bandwidth usage.

Remove the image from memory if the image widget has been disposed to reduce device memory usage.

High performance due to the use of dart:isolate

Usage

Setting up

All you have to do is Initializing the cache and wrap the root widget with DisposableImages.

void main() {
  await DisposableImages.init();

  runApp(const DisposableImages(MyApp()));
}

Now your app is ready to use the package.

initialization options
maximumDecodedImagesCount

The maximum number of decoded images that should be kept in memory when using DisposableImages.decodedImages

maximumDecode

The maximum number of images to be decoded simultaneously

maximumDownload

The maximum number of images to be downloaded simultaneously

Displaying images

Use DisposableCachedImage named constructors to display images.

Obtaining an image from a URL
DisposableCachedImage.network(imageUrl: 'https://picsum.photos/id/23/200/300');
Obtaining a local image from assets using path
DisposableCachedImage.asset(imagePath: 'assets/images/a_dot_burr.jpeg');
Obtaining a local image from device storage using path
DisposableCachedImage.local(imagePath: 'assets/images/a_dot_burr.jpeg');
Obtaining an image from Uint8List
DisposableCachedImage.bytes(bytes: bytes);
Display dynamic height images

If you are using dynamic height images, you should declare this as shown below to avoid UI jumping

DisposableCachedImage.network(
  imageUrl: imageUrl,
  isDynamicHeight: true,
  width: MediaQuery.of(context).size.width * 0.3,
),

width required when displaying dynamic height images


You can display your custom widgets while the image is loading, downloading, has an error and when it is ready as shown below

DisposableCachedImage.network(
  imageUrl: 'https://picsum.photos/id/23/200/300',
  onLoading: (context) => const Center(
    child: Icon(Icons.downloading),
  ),
  progressBuilder: (context, progress) => Center(
    child: LinearProgressIndicator(value: progress),
  ),
  onError: (context, error, stackTrace, retryCall) => Center(
    child: IconButton(
      onPressed: retryCall,
      icon: const Icon(Icons.download),
    ),
  ),
  onImage: (context, imageWidget, height, width) => Stack(
    children: [
      imageWidget,
      MyWidget(),
    ],
  ),
);
Resize images

You can change the image size to the provided width and \ or height by enabling resizeImage (disabled by default) to reduce raster thread usage when using high resolution images, if this option is enabled the same provider instance would be used for the number of images that share the same url \ path with a different image size for each widget to have a gallery like experience.

To change the size of the images in bytes before they are saved to the storage, provide maxCacheWidth and \ or maxCacheHeight.

DisposableCachedImage.network(
 imageUrl: imageUrl,
 maxCacheWidth: 300,
 width: 200,
 resizeImage: true,
),
Clipping

You can clip the image either with rounded corners by providing BorderRadius

DisposableCachedImage.network(
  imageUrl: imageUrl,
  borderRadius: const BorderRadius.all(Radius.circular(20)),
),

Or by setting BoxShape to get oval image

DisposableCachedImage.network(
  imageUrl: imageUrl,
  shape: BoxShape.circle,
),
Keeping images alive

By default each image is removed from memory when it is not being used by any widget, however you can keep some images in memory for the entire application lifecycle as shown below

DisposableCachedImage.network(
  imageUrl: imageUrl,
  keepAlive: true,
),

The other arguments are similar if not quite the same as Image Widget


Decode images before displaying

You can use DisposableImages.decodedImages to decode images before displaying them in case you need to show images immediately without loading

  // decode network images
  DisposableImages.decodedImages.addNetwork(url);

  // decode loacl images
  DisposableImages.decodedImages.addLocal(path);

  // decode assets images
  DisposableImages.decodedImages.addAssets(path);

If you decode the images in this way, you will have to dispose the images manually or set the maximum number of decoded images

// set the maximum number of decoded images, old images will be removed if this number is exeeded
// this is optional and can be null
DisposableImages.init(maximumDecodedImagesCount: 100);


// dispose an image by key
// the key is path if the image is local or assets, if it's a network image the key is the url
DisposableImages.decodedImages.dispose(key);

// dispose all images
DisposableImages.decodedImages.disposeAll();
Remove all cached images from the device storage
DisposableCachedImage.clearCache();

How it works

The package uses RawImage with dart-ui-Image directly without the need for ImageProvider

Stores and retrieves files using dart:io.

Disposing and changing image state using flutter_riverpod with state_notifier.

Downloading images with dart:_http on other platforms.

Example app

The example directory has a sample application that uses this plugin.

Roadmap

Improve package documentation

Further improvements

所屬分類

依賴

依賴清單 4 項

原始碼儲存庫

使用此套件的專案

暫無相關資料。