image_decoder.dart 1.03 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5

6
import 'dart:typed_data';
7 8 9
import 'dart:ui' as ui show Codec, FrameInfo, Image;

import 'binding.dart';
10

11 12 13 14
/// Creates an image from a list of bytes.
///
/// This function attempts to interpret the given bytes an image. If successful,
/// the returned [Future] resolves to the decoded image. Otherwise, the [Future]
15
/// resolves to null.
16 17 18 19 20 21 22 23
///
/// If the image is animated, this returns the first frame. Consider
/// [instantiateImageCodec] if support for animated images is necessary.
///
/// This function differs from [ui.decodeImageFromList] in that it defers to
/// [PaintingBinding.instantiateImageCodec], and therefore can be mocked in
/// tests.
Future<ui.Image> decodeImageFromList(Uint8List bytes) async {
24
  final ui.Codec codec = await PaintingBinding.instance!.instantiateImageCodec(bytes);
25 26
  final ui.FrameInfo frameInfo = await codec.getNextFrame();
  return frameInfo.image;
27
}