[英]Load pictures from local storage > OutofmemoryException - C# - Xamarin.Forms
When i want to load pictures from internal storage, sometimes its to laggy but sometimes i get OutOfMemmoryException. 当我想从内部存储中加载图片时,有时会变得迟钝,但有时会出现OutOfMemmoryException。
var picList = System.Instance.GetFiles("/storage/emulated/0/DCIM/Camera", true); //Its a string list, include the files name
var inc = 0;
foreach (var item in picList)
{
var byteArray = FileSystem.Instance.ReadFile("/storage/emulated/0/DCIM/Camera", item.Split('/').Last(), true);
var toPicture = ImageSource.FromStream(() => new MemoryStream(byteArray));
var image = new Image
{
ClassId = inc.ToString(),
Source = toPicture,
WidthRequest = 200,
HeightRequest = 200,
};
` }
}
I think i need to dispose it but i dont know how. 我想我需要处置它,但我不知道如何。
Most likely you never invoke image.Dispose()
method on image instance so memory allocated to image is never released. 很可能您永远不会在图像实例上调用
image.Dispose()
方法,因此永远不会释放分配给图像的内存。 Also you should consider not loading all files but load them on demand. 另外,您应该考虑不加载所有文件,而是按需加载它们。
I think you should declare your variables outside the loop and reuse them, instead of declaring new ones at each iteration : 我认为您应该在循环外声明变量并重用它们,而不是在每次迭代时声明新变量:
byte[] byteArray = null;
MemoryStream toPicture = null;
Image image = null;
foreach (var item in picList)
{
byteArray = FileSystem.Instance.ReadFile("/storage/emulated/0/DCIM/Camera", item.Split('/').Last(), true);
toPicture = ImageSource.FromStream(() => new MemoryStream(byteArray));
image = new Image
{
ClassId = inc.ToString(),
Source = toPicture,
WidthRequest = 200,
HeightRequest = 200,
};
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.