imageBytes method

Future<Uint8List> imageBytes(
  1. String id,
  2. int height,
  3. int width, {
  4. int? compression,
})

Returns a version of the image at the given size in a jpeg format suitable for loading with MemoryImage.

The returned image will maintain its aspect ratio while fitting within the given dimensions height, width. The id to use is available from a returned LocalImage. The image is sent from the device as a JPEG compressed at 0.7 quality, which is a reasonable tradeoff between quality and size. If displayed images look blurry or low quality try requesting more pixels, i.e. a larger value for height and width. Instead of using this directly look at DeviceImage which creates an ImageProvider from a LocalImage, suitable for use in a widget tree.

Implementation

Future<Uint8List> imageBytes(String id, int height, int width,
    {int? compression}) async {
  if (!_initWorked) {
    throw LocalImageProviderNotInitializedException();
  }
  _stopwatch.reset();
  _stopwatch.start();
  final Uint8List? photoBytes = await (LocalImageProviderPlatform.instance
      .imageBytes(id, height, width, compression: compression));
  _stopwatch.stop();
  _totalLoadTime += _stopwatch.elapsedMilliseconds;
  _lastLoadTime = _stopwatch.elapsedMilliseconds;
  if (null != photoBytes) {
    _bytesLoaded += photoBytes.length;
    return photoBytes;
  }
  return Uint8List(0);
}