简体   繁体   English

如何从 SVG 字符串中预缓存 SVG 图像?

[英]How to precache a SVG image from a SVG string?

I'm already using the precachePicture function from the flutter_svg library, to load svg assets:我已经在使用flutter_svg库中的precachePicture function 来加载 svg 资产:

Future<void> loadPictures() async {
  await precachePicture(
    ExactAssetPicture(SvgPicture.svgStringDecoder, 'assets/graphic/two_people.svg'),
    context,
  ); 
}

However i have some raw svg string (more complexe than this one) that need to be precached too但是我有一些原始的 svg 字符串(比这个更复杂)也需要预先缓存

<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 240 240"><polygon points="120 8.9 147.31 93.45 236.99 93.45 164.41 146.38 191.77 231.09 120 178.76 48.23 231.09 75.59 146.37 3.01 93.45 92.69 93.45 120 8.9" fill="#f8d64e"/></svg>

How can one precache a SVG image from a SVG string?如何从 SVG 字符串中预缓存 SVG 图像?

So, i finally found the solution:所以,我终于找到了解决方案:

svgString = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 240 240"><polygon points="120 8.9 147.31 93.45 236.99 93.45 164.41 146.38 191.77 231.09 120 178.76 48.23 231.09 75.59 146.37 3.01 93.45 92.69 93.45 120 8.9" fill="#f8d64e"/></svg>';

Future<void> loadSvgPictureFromSvgString() async {
  await precachePicture(
            StringPicture(SvgPicture.svgStringDecoder, svgString), context); //context or null
}
  

Now, when you call SvgPicture.string(svgString) the image would already be precached, so no delay of rendering if the svgString is a bit complexe.现在,当您调用SvgPicture.string(svgString)时,图像已经被预缓存,因此如果 svgString 有点复杂,则不会延迟渲染。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM