loading images from server

LarrySeymourLarrySeymour USMember ✭✭

I am trying to retrieve an image that is stored on my webhost.

                    foreach (BusinessMeals item in _mealsList)
                    {
                        UIImage bitImage = null;
                        String uri = "http://myAddress/meals/" + item.ImageName;
                        using (var url = new NSUrl(uri))
                        using (var data = NSData.FromUrl(url))
                            bitImage = UIImage.LoadFromData(data);

                        item.Image = bitImage;
                    }

My DATA var always returns null. is this code correct. I know the url is correct as I have checked the url that is created and it works.

Tagged:

Best Answer

Answers

  • JohnMillerJohnMiller USForum Administrator, Xamarin Team Xamurai

    @LarrySeymour,

    You may be hitting an issue related to ATS and using http instead of https. Usually the Application Output while running will have a message in it related to this.

  • LarrySeymourLarrySeymour USMember ✭✭

    @JohnMiller i actually just saw this while searching, I see I have to modify my plist with

    NSAppTransportSecurity

    NSAllowsArbitraryLoads

    I don't see it in VS so I am doing a manual insert.

  • LarrySeymourLarrySeymour USMember ✭✭
    Accepted Answer

    the code to allow non secure ot http downloads can be found here
    https://developer.xamarin.com/guides/ios/application_fundamentals/ats/
    its the last option at bottom of the page.

  • SaiLokeshSaiLokesh INMember ✭✭
    edited August 2017

    Try this @LarrySeymour
    `public static UIImage GetImageFromUrl(string url)
    {
    NSData imgData = null;
    UIImage img = null;
    try
    {
    NSUrl imageURL = new NSUrl(url);
    imgData = NSData.FromUrl(imageURL);
    img = UIImage.LoadFromData(imgData);

            }
            catch (Exception exe)
            {
                Console.Writeline(exe.message)
            }
            return img;
        }`
    

    this code snippet might help,

Sign In or Register to comment.