Xamarin形式:ImageLoaderSourceHandler:无法检索图像或图像数据无效: [英] Xamarin forms: ImageLoaderSourceHandler: Could not retrieve image or image data was invalid:

查看:334
本文介绍了Xamarin形式:ImageLoaderSourceHandler:无法检索图像或图像数据无效:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在UI上显示图像时出现以下错误消息.

Getting below error message when trying to show an image on 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上显示图像:

I am using below code for showing image on UI:

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

问题仅与android项目有关.经历一些类似的问题线程,但没有一个对我有帮助.

The issue is only with the android project. Go through some similar issue threads, but none of them helps me.

推荐答案

从远程URL加载图像在Android上不是很友好.因此,我建议您使用插件 FFImageLoading . FFImageLoading for Forms提供了CachedImage-直接替换Image类.用法相同,但还具有一些其他属性.

Load image from remote url is not very friendly on Android . So I suggest that you ca use the plugin FFImageLoading . FFImageLoading for Forms provides CachedImage - a direct Image class replacement. It’s used just the same but it has some additional properties.

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

Firstly,Add the following NuGet packages to every project(iOS and Android) in your solution.

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)

然后在初始化表单之前在单独的平台中初始化渲染器.

Then init the renderer in sepecfic platforms before you init the forms.

在iOS中-> AppDelegate.cs

in iOS -> AppDelegate.cs

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

在Android中-> MainActivity.cs

in Android ->MainActivity.cs

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

xaml

<?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不仅显示图像在Android 上.

And here is a similar case that you can check ImageCell not displaying image only on Android .

这篇关于Xamarin形式:ImageLoaderSourceHandler:无法检索图像或图像数据无效:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆