Search code examples
androidiosxamarin.formsakavache

LoadImageFromUrl IBitmap to ImageSource


I am using the code below the Akavache to cache images.

Return is an IBitmap, how can I convert this IBitmap to an ImageSource?

var url = "https://ashdbhjas/image.png";
ImageSource imageSrc = await BlobCache.LocalMachine.LoadImageFromUrl(url); // ???

Solution

  • Try this:

    var url = "https://ashdbhjas/image.png";
    var img = await BlobCache.LocalMachine.LoadImageFromUrl(url); 
    
    MemoryStream imageStream = new MemoryStream();
    
    await img.Save(CompressedBitmapFormat.Jpeg, 1.0f, imageStream);
    
    stream.Position = 0;
    var imageSrc = ImageSource.FromStream(() => imageStream);
    

    Basically you are saving the IBitmap into a MemoryStream then using this to create your ImageSource object of your Image.

    Hope this helps!