Most efficient way to load large local images in Flutter

Viewed 268

In my app, I need to load more than 100 photos from phone gallery to a small resized (maybe also cropped) flutter ui.Image so I can manipluate them in Canvas.

I searched a lot for an efficient way to do this, but it's still not fast enough. Loading these photos which have an average 3000x3000px dimensions to a smaller 300x300px version takes many seconds. It's about 6x times slower than what I can achieve using Glide or the feature inSampleSize of a Bitmap in Android.

Here's how I am loading a resized version of an image from Uint8List:

static Future<ui.Image> loadToResizedImage(AssetEntity asset, int neededHeight) async {
  final Uint8List? bytes = await asset.originBytes;
  int width = asset.width;
  int height = asset.height;

  ui.Codec codec = await ui.instantiateImageCodec(bytes!, targetHeight: neededHeight, allowUpscaling: false);

  // The following line takes about 90% of the time needed in this function
  return await codec.getNextFrame();
}
0 Answers
Related