loadGif static method
Loads a GifAtlas
instance from the given asset path
. If a cacheId
is
provided, the resulting GifAtlas
is stored in the cache with the given
ID. If the cacheId
already exists in the cache, the cached GifAtlas
is
returned. The resolution
parameter controls the scaling of the gif's
frames.
Implementation
static Future<GifAtlas> loadGif(
String path, {
double resolution = 1.0,
String? cacheId,
}) async {
if (cacheId != null && gifCache.containsKey(cacheId)) {
return gifCache[cacheId]!;
}
final data = await rootBundle.load(path);
final bytes = Uint8List.view(data.buffer);
final codec = await ui.instantiateImageCodec(bytes, allowUpscaling: false);
final atlas = GifAtlas();
atlas.scale = resolution;
atlas.numFrames = codec.frameCount;
for (var i = 0; i < atlas.numFrames; ++i) {
final frame = await codec.getNextFrame();
final texture = GTexture.fromImage(frame.image, resolution);
var gifFrame = GifFrame(frame.duration, texture);
atlas.addFrame(gifFrame);
}
if (cacheId != null) {
gifCache[cacheId] = atlas;
}
return atlas;
}