jamesmontemagno / ImageCirclePlugin

Circle Images for your Xamarin.Forms Applications
MIT License
239 stars 68 forks source link

Bound ImageSource is not cached on UWP #14

Closed CodePhobie closed 5 years ago

CodePhobie commented 7 years ago

Version Number of Plugin: 1.8.0.5 Device Tested On: UWP on Windows 10

Expected Behavior

The stream of the ImageSource should be read only once (and cached) in UWP app; like in Android app; even when the size of the CircleImage changes

Actual Behavior

When binding the CircleImage.ImageSource, the stream gets requested every time the size of the CircleImage changes (only in UWP app, not in Android app); for instance when changing the device orientation; the requested stream gets every time displosed and has to be recreated

Steps to reproduce the Behavior

  1. Add an CircleImage to a grid with dynamic layout so that the hight or width changes when the device orientation changes
  2. Bind the ImageSource to a property that uses Image.FromFile(Func)
  3. When running the app on Android the steam of the ImageSource is requested only once (even when rotating the device); while on UWP the stream is requested every time the Size of the CircleImage changes

Workaround

A Workaround is to cache the stream in your code and return every time a new copy of the stream; because the stream gets displosed after reading:

var stream = file.GetStream();
file.Dispose();
var memoryStream = new System.IO.MemoryStream((int)stream.Length);
stream.CopyTo(memoryStream);

ProfilePicture = ImageSource.FromStream(
    () =>
    {
        memoryStream.Seek(0, System.IO.SeekOrigin.Begin);
        var copy = new System.IO.MemoryStream((int)memoryStream.Length);
        memoryStream.WriteTo(copy);
        copy.Seek(0, System.IO.SeekOrigin.Begin);

        return copy;
    });

This workaround uses alot of memory at runtime

jamesmontemagno commented 5 years ago

Fixed