encodeImage method

Uint8List encodeImage({
  1. EncodeImageOptions? options,
})

Returns the stored image encoded with the given options.

Implementation

Uint8List encodeImage({EncodeImageOptions? options}) {
  if (_buffer != null && options == null) {
    return _buffer!;
  }

  options ??= EncodeImageOptions();
  return using((Arena arena) {
    Pointer<Utf8> pUUID = uniqueId!.toNativeUtf8(allocator: arena);

    Pointer<CEncodeImageOptions> pConfig = arena<CEncodeImageOptions>();

    EncodeImageOptions resolvedOptions = options ?? EncodeImageOptions();
    pConfig.ref.format = resolvedOptions.format.index;
    pConfig.ref.quality = resolvedOptions.quality;

    Pointer<Opaque> opaqueBuffer = _nativeEncodeImage(pUUID, pConfig);

    Pointer<Uint8> pData = _nativeOpaqueBufferDataGetter(opaqueBuffer);
    int size = _nativeOpaqueBufferSizeGetter(opaqueBuffer);

    // TODO use finalizer and prevent copying as soon as min sdk >= 3.1
    Uint8List view = pData.asTypedList(size);
    Uint8List ret = Uint8List(view.length);
    ret.setAll(0, view);
    _nativeOpaqueBufferFree(opaqueBuffer);
    return ret;
  });
}