9

How to Resolve ImageLoaderSourceHandler: Could Not Retrieve Image or Image Data...

 3 years ago
source link: https://doumer.me/how-to-resolve-imageloadersourcehandler-could-not-retrieve-image/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
  • .Net , Xamarin , Xamarin Forms Tips and Tricks , Xamarin.Forms ,
  • 30 Jun

Hey friends, today we will take a quick look at a little, but annoying problem we still face in Xamarin Forms. I guess you might have come across this error message “[0:] ImageLoaderSourceHandler: Could not retrieve image or image data was invalid: Uri:“.

This error is quite simple to identify. Here is how it manifests itself. You want to display an image in your app from an external source on the internet, you bind the URL to an image view, or pass it directly, but once you run your app the image doesn’t appear. In my experience, this error occurs when you try to display an image in Xamarin Forms from a source that does not have an SSL-certified URL. This error might be caused by some other complex things that I don’t yet know, but in my case, it occurred when I passed URLs with the “HTTP:” instead of “HTTPS:”.

So, in this short post, I share with you how you can overcome the error described above very easily. To overcome it, we will create a value converter that downloads the image for us. Once we have the image, we use a memory stream to pass the image to the image view via the converter. And that is all. So, here is the code.

The value converter to resolve ImageLoaderSourceHandler: Could Not Retrieve Image or…

    public class ImageSourceFromStreamConverter : IValueConverter
        public object Convert(object value, Type targetType, object parameter, CultureInfo cultureInfo)
            var source = value as string;
            if (string.IsNullOrEmpty(source))
                return null;
            var decodedUrl = HttpUtility.UrlDecode(source, Encoding.UTF8);
            var byteImage = DownlodaImage(decodedUrl);
            var stream = new MemoryStream(byteImage);
            return ImageSource.FromStream(() => stream);
        byte[] DownlodaImage(string url)
            using (WebClient webClient = new WebClient())
                byte[] data = webClient.DownloadData(url);
                return data;
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo cultureInfo)
            throw new NotImplementedException();

Now, to apply this converter is very easy. To learn more about value converters, check these docs. This is how we do it.

        <Image Source="{Binding ImageSrc, Source={RelativeSource AncestorType={x:Type app:MainPage}}, Converter={StaticResource ImgSourceFromStreamValueConverter}}"/>

Conclusion

That is all. You can just copy and paste this value converter whenever you come across this issue. As simple as that. Hey, if you like my articles and videos, please don’t forget to like and subscribe to my channel and blog for more tips and tricks for software dev, especially dotnet dev. You might also like this post about this super powerfull media player for Xamarin Forms.

Follow me on social media and stay updated

Like this:


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK