locked
UriImageSource-based image not rendering RRS feed

  • Question

  • User55850 posted

    Hello,

    I am creating an image from a Uri, but it doesn't render in my app at all (note that local images work fine!). I believe everything is set up correctly, so I'm a little confused as to the cause of the problem!

    private static Image GenerateImage(string imagePath) { Image image = new Image() { Source = ImageSource.FromUri(new Uri(imagePath)) }; return image; }

    Any ideas as to what is causing the problem? That code doesn't appear to reveal that much, as it sets the source correctly.

    Thanks, Richard

    Friday, July 25, 2014 12:53 PM

Answers

  • User28549 posted

    If this is on iOS from visual studio, there are a number of us that can confirm this issue.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, July 25, 2014 1:31 PM

All replies

  • User28549 posted

    If this is on iOS from visual studio, there are a number of us that can confirm this issue.

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Friday, July 25, 2014 1:31 PM
  • User55850 posted

    Yes I believe I have just seen your post.

    As a result of that post, I tried a build from Xamarin Studio on Mac, and it works through that! Unsure of what is causing the issue. Bit annoying, as I have just spent the past hour trying to debug an issue with my code!

    I assume some sort of bug report has been sent through already?

    Friday, July 25, 2014 1:37 PM
  • User21647 posted

    I bumped my head on this one too. Ironically it was Android that told me what the problem actually was. I was trying to do exactly the same thing via xaml binding, it looked something like this:

    ```

    ```

    If I set the Image source to the url directly, it worked. If I tried to bind the UriImageSource to a property tho, it would never load/render the image. I really wanted to use the UriImageSource because of the built-in caching.

    When I fired up the same thing on Android, it failed immediately, telling me it couldn't convert the url string in my viewmodel's property, to a Uri. Weird.

    So, I created a quick and dirty StringToUriConverter to do just that, added it to the binding, and voila, it worked!

    Here's the code for the StringToUriConverter, which may save you a couple of minutes:

    ``` public class StringToUriConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { if(value == null) return string.Empty;

            string data = value as string;
    
            if(data == null || data.IsEmpty() || !data.StartsWith("http", StringComparison.OrdinalIgnoreCase))
            {
                string message = string.Format("StringToUriConverter expects a string starting with http(s). You gave it '{0}'", value);
                // LogDebug(message);
                return string.Empty;
            }
    
            return new Uri(data);
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException("Converting a Uri back to a string isn't supported (yet) ");
        }
    }
    

    ```

    I then added it to my app xaml resource dictionary, so it would be accessible from anywhere: <Application.Resources> <ResourceDictionary> <helpers:StringToUriConverter x:Key="StringToUriConverter" /> </ResourceDictionary> </Application.Resources>

    and then added it as a converter inside my binding:

    ```

    ```

    HTH someone :smile:

    Friday, September 25, 2015 2:32 AM
  • User97779 posted

    I had same frustration but did something easier on the DTO level:

    public class MyDto { public string MyImageUrl {get;set;} public string MyImageUri {get{return new Uri(MyImageUrl);}} }

    And voila: <StackLayout Grid.Row="1" Grid.Column="1" Padding="0" Orientation="Horizontal"> <Image HeightRequest="48" WidthRequest="48" Aspect="AspectFit" VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand"> <Image.Source> <UriImageSource Uri="{Binding Path=Note.MyImageUri}" /> </Image.Source> </Image> </StackLayout>

    Tuesday, April 19, 2016 8:39 PM
  • User266487 posted

    @Korayem - Thanks. I ended up returning a UriImageSource directly from my ViewModel.

    `public String ImageURL { get; set; }

        [JsonIgnore] // -- no need to serialise this when saving to database
        public UriImageSource ImageSource
        {
            get
            {
                return new UriImageSource
                {
                    Uri = new Uri(ImageURL),
                    CachingEnabled = true,
                    CacheValidity = new TimeSpan(3, 0, 0, 0) // -- cache for 3 days
                };
            }
        }`
    

    @JohnWilson.4976 - Thank you. I'm trying to use a TypeConverter for something else.

    In your app.xaml did you specify the namespace or did you create your StringToUriConverted class in App.xaml.cs ?

    I tried adding xmlns:helpers="using:MyNamespace in App.xaml but it's not happy. When debugging it's throwing Type helpers:PriceToStringConverter not found in xmlns using:MyNamespace

    Friday, October 28, 2016 6:34 PM
  • User266487 posted

    I tried moving it back in to the page resources, rather than App.xaml and amending the xml namespace to local. That seems to have done the trick.

    xmlns:local="clr-namespace:MyNameSpace;assembly=MyNameSpace"

    Sunday, October 30, 2016 10:44 AM
  • User21647 posted

    @RD8 ok, glad you got it working.

    For the sake of answering your question tho, I put my converter into a shared PCL helper class library (coz I use it elsewhere too).

    Then in my xaml page where I need to use it, I make a converters namespace, like this

    ```

    <!--  snip  irrelevant stuff -->
             xmlns:abstractions="clr-namespace:ImageCircle.Forms.Plugin.Abstractions;Assembly=ImageCircle.Forms.Plugin.Abstractions"
    
             xmlns:converters="clr-namespace:My.Mobile.UI.Views.Converters;assembly=My.Mobile.UI"
             x:Class="My.Mobile.UI.App"
             >
    
    <Application.Resources>
     <ResourceDictionary>
    
          <converters:StringToUriConverter x:Key="StringToUriConverter" />
          <converters:ReverseBoolValueConverter x:Key="ReverseBoolValueConverter" />
          <converters:HtmlSourceConverter x:Key="HtmlSourceConverter" />
    
     </ResourceDictionary>
    </Application.Resources>
    

    ```

    Where My.Mobile.UI is the name of the PCL project (and namespace)

    Note that this is from a slightly different project, so the namespaces are a bit different, i couldn't find the one where I had the issue originally.

    Sunday, October 30, 2016 9:10 PM
  • User266487 posted

    Thank you.

    Thursday, November 3, 2016 1:26 PM