首页 > 解决方案 > 如何使用 FFImageLoading 通过 Xamarin Forms 5 成功缓存和加载 svg 资源

问题描述

我正在尝试像这样使用 FFImageLoading:

应用委托:

CachedImageRenderer.Init();
CachedImageRenderer.InitImageSourceHandler();
var config = new FFImageLoading.Config.Configuration()
{
    VerboseLogging = true,
    VerbosePerformanceLogging = false,
    VerboseMemoryCacheLogging = false,
    VerboseLoadingCancelledLogging = false
};
ImageService.Instance.Initialize(config);
var ignore = typeof(FFImageLoading.Svg.Forms.SvgCachedImage);

预加载:

    public void PreloadImages()
    {
        try
            var resources = Assembly.GetExecutingAssembly().GetManifestResourceNames();
            foreach (var r in resources)
            {
                if (!r.EndsWith(".svg")) continue;
                try
                {
                    var assemblyName = Application.Current?.GetType()?.GetTypeAssemblyFullName();
                    ImageService.Instance.LoadEmbeddedResource($"resource://{r}?assembly={Uri.EscapeUriString(assemblyName)}")
                    //ImageService.Instance.LoadEmbeddedResource($"resource://{r}?assembly={Assembly.GetExecutingAssembly().GetName().Name}")
                    .Success((info, result) =>
                    {
                        Debug.WriteLine($"Preloading success! Key: {info.CacheKey}");
                    })
                    .Preload();
                }
                catch (Exception ex)
                {
                    Debug.WriteLine($"LoaderPage.PreloadImages[{r}]", ex);
                }
            }
    }

当代码运行时,图像出现在屏幕上,但我的应用程序输出充满了如下消息:

System.BadImageFormatException: Can't read image size properties. File corrupted?
  at FFImageLoading.Decoders.GifDecoder.SourceRegfToDecodedImageAsync (Foundation.NSData nsdata, CoreGraphics.CGSize destSize, System.nfloat destScale, FFImageLoading.Config.Configuration config, FFImageLoading.Work.TaskParameter parameters, FFImageLoading.Decoders.GifDecoder+RCTResizeMode resizeMode, FFImageLoading.Work.ImageInformation imageinformation, System.Boolean allowUpscale) [0x00068] in C:\projects\ffimageloading\source\FFImageLoading.Shared.IosMac\Decoders\GifDecoder.cs:62 
  at FFImageLoading.Decoders.GifDecoder.DecodeAsync (System.IO.Stream stream, System.String path, FFImageLoading.Work.ImageSource source, FFImageLoading.Work.ImageInformation imageInformation, FFImageLoading.Work.TaskParameter parameters) [0x000e4] in C:\projects\ffimageloading\source\FFImageLoading.Shared.IosMac\Decoders\GifDecoder.cs:45 
  at FFImageLoading.Work.ImageLoaderTask`3[TDecoderContainer,TImageContainer,TImageView].GenerateImageAsync (System.String path, FFImageLoading.Work.ImageSource source, System.IO.Stream imageData, FFImageLoading.Work.ImageInformation imageInformation, System.Boolean enableTransformations, System.Boolean isPlaceholder) [0x0007c] in C:\projects\ffimageloading\source\FFImageLoading.Common\Work\ImageLoaderTask.cs:337 
  at FFImageLoading.Work.ImageLoaderTask`3[TDecoderContainer,TImageContainer,TImageView].RunAsync () [0x0047c] in C:\projects\ffimageloading\source\FFImageLoading.Common\Work\ImageLoaderTask.cs:643
2021-02-24 01:50:30.338196+0900 Memorise.iOS[41119:898256] Image loading failed: resource://Memorise.Resources.Cards.pause_Light.svg?assembly=Memorise,%20Version=1.0.0.0,%20Culture=neutral,%20PublicKeyToken=null
System.BadImageFormatException: Can't read image size properties. File corrupted?

请注意,一些 SVG 非常简单,我不知道它们为什么或如何损坏,以及屏幕上的显示。

有没有人能够让预加载工作。请注意,我尝试了两种不同的方法,LoadEmbeddedResource但都没有奏效。

标签: xamarinxamarin.formsffimageloading

解决方案


推荐阅读