How to convert Assets Images & Icons to PdfImage i

2020-07-18 09:06发布

问题:

Used Library: dart_pdf After searching I found the same issue in GITHUB but unable to resolve the issue. I tried this but blurry image appears. Please help!!

  ByteData data = await rootBundle.load('assets/test.jpg');
  var codec = await instantiateImageCodec(data.buffer.asUint8List());
  var frame = await codec.getNextFrame();

  var imageBytes = await frame.image.toByteData();
  PdfImage assetImage = PdfImage(pdf.document,
      image: imageBytes.buffer.asUint8List(), width: 86, height: 80);

Rendered Image:

回答1:

Use this instead:

final PdfImage assetImage = await pdfImageFromImageProvider(
    pdf: pdf.document, 
    image: const AssetImage('assets/test.jpg'),
);