Download image from internet and save it to sqlite database

I want to set the UIImageview image from an internet link. and save this image after to sqlite

the scenario is like this:

imageview.image = downloadimage();

void downloadimage(){
if (image is available in my sqlite db){
load image image from sqlite (base64)
}
else{
download image from website
save to sqlite (base64)
}
}

my questions:
1. how to download image ---> NSData.FromUrl(url) return always null even if image is available
2. covert image to base 64
3. get image from base 64

I am new to xamarin ios. i have the the scenario above on android and it s so easy for me

Best Answer

  • ColeXColeX Xamurai
    edited November 2018 Accepted Answer

    how to download image ---> NSData.FromUrl(url) return always null even if image is available

    https://stackoverflow.com/a/49506212/8187800

    covert image to base 64

     NSData data = Yourimage.Image.AsPNG();
     string base64String = data.GetBase64EncodedString(NSDataBase64EncodingOptions.None);
    

    get image from base 64

     NSData data = new NSData(base64String, NSDataBase64DecodingOptions.IgnoreUnknownCharacters);
     UIImage image = new UIImage(data);
    

Answers

  • ColeXColeX Member, Xamarin Team Xamurai
    edited November 2018 Accepted Answer

    how to download image ---> NSData.FromUrl(url) return always null even if image is available

    https://stackoverflow.com/a/49506212/8187800

    covert image to base 64

     NSData data = Yourimage.Image.AsPNG();
     string base64String = data.GetBase64EncodedString(NSDataBase64EncodingOptions.None);
    

    get image from base 64

     NSData data = new NSData(base64String, NSDataBase64DecodingOptions.IgnoreUnknownCharacters);
     UIImage image = new UIImage(data);
    
  • XAMARINROCKXAMARINROCK Member ✭✭

    Thank you colex for your reply.

    I tried the NSUrlSession before and it didn't work on the link that contains my images.
    when I tried today after getting your response on a google gallery image, it works fine.

    The difference between the 2 links, is that mine is not secured and without certificate (I need to click proceed on google chrome to open it)
    I think that is the problem.
    Is there a way to skip the security and open the my images??
    Thank you again

Sign In or Register to comment.