【问题标题】:Xamarin forms: ImageLoaderSourceHandler: Could not retrieve image or image data was invalid:Xamarin 表单:ImageLoaderSourceHandler:无法检索图像或图像数据无效:
【发布时间】:2020-02-06 12:45:48
【问题描述】:

尝试在 UI 上显示图像时收到以下错误消息。

异常详情:

[0:] ImageLoaderSourceHandler: Could not retrieve image or image data was invalid: Uri: "imageurl"
[0:] Image Loading: Error getting stream for "imageurl": System.ObjectDisposedException: Can not access a closed Stream.
  at System.IO.StreamHelpers.ValidateCopyToArgs (System.IO.Stream source, System.IO.Stream destination, System.Int32 bufferSize) [0x0003a] in <46c2fa109b574c7ea6739f9fe2350976>:0 
  at System.IO.MemoryStream.CopyToAsync (System.IO.Stream destination, System.Int32 bufferSize, System.Threading.CancellationToken cancellationToken) [0x00000] in <46c2fa109b574c7ea6739f9fe2350976>:0 
  at Xamarin.Forms.UriImageSource.GetStreamAsyncUnchecked (System.String key, System.Uri uri, System.Threading.CancellationToken cancellationToken) [0x00310] in D:\a\1\s\Xamarin.Forms.Core\UriImageSource.cs:185 
  at Xamarin.Forms.UriImageSource.GetStreamFromCacheAsync (System.Uri uri, System.Threading.CancellationToken cancellationToken) [0x00140] in D:\a\1\s\Xamarin.Forms.Core\UriImageSource.cs:209 
  at Xamarin.Forms.UriImageSource.GetStreamAsync (System.Uri uri, System.Threading.CancellationToken cancellationToken) [0x000f0] in D:\a\1\s\Xamarin.Forms.Core\UriImageSource.cs:139 
  at Xamarin.Forms.UriImageSource.GetStreamAsync (System.Threading.CancellationToken userToken) [0x00056] in D:\a\1\s\Xamarin.Forms.Core\UriImageSource.cs:78 
Debugger Connection Lost: Debugger lost connection to the running application. Likely this means the application terminated unexpectedly.

版本详情

Xamarin forms Version: 4.2.0.848062
Target Framework: Android 9.0 Pie
Target Android Version: Android 9.0(API Level 28 - Pie)

我正在使用以下代码在 UI 上显示图像:

image.Source = ImageSource.FromUri(new Uri(ImageUrl));

问题仅在于 android 项目。浏览一些类似的问题线程,但没有一个对我有帮助。

【问题讨论】:

    标签: image xamarin.forms


    【解决方案1】:

    从远程 url 加载图像在 Android 上不是很友好。所以我建议你可以使用插件 FFImageLoadingFFImageLoading for Forms 提供了 CachedImage - 一个直接的 Image 类替换。它的用法相同,但有一些额外的属性。

    首先,将以下 NuGet 包添加到您解决方案中的每个项目(iOS 和 Android)

    Xamarin.FFImageLoading
    
    Xamarin.FFImageLoading.Forms,
    
    Xamarin.FFImageLoading.Transformations (if you’ll use transformations like CircleTransformation)
    
    Xamarin.FFImageLoading.Svg (for SVG support)
    
    Xamarin.FFImageLoading.Svg.Forms (for SVG support)
    
    SkiaSharp (for SVG support)
    

    然后在初始化表单之前在特定平台中初始化渲染器。

    在 iOS -> AppDelegate.cs

    FFImageLoading.Forms.Platform.CachedImageRenderer.Init();  
    

    在 Android 中 ->MainActivity.cs

    FFImageLoading.Forms.Platform.CachedImageRenderer.Init(enableFastRenderer: true);
    

    在xml中

    <?xml version="1.0" encoding="UTF-8"?>
    <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
        x:Class="FFImageLoading.Forms.Sample.Pages.XamlSimpleExamplePage"
        xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"
        Title="Basic XAML Example">
        <ContentPage.Content>
            <ffimageloading:CachedImage HorizontalOptions="Center" VerticalOptions="Center"
                WidthRequest="300" HeightRequest="300"
                DownsampleToViewSize="true"
                Source = "{Binding xxx}">
            </ffimageloading:CachedImage>
        </ContentPage.Content>
    </PFContentPage>
    

    这里有一个类似的案例,你可以查看ImageCell not displaying image only on Android

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-20
      • 2014-07-02
      • 2020-05-12
      • 2018-06-17
      • 2013-11-09
      • 1970-01-01
      • 2020-09-29
      相关资源
      最近更新 更多