当我想从内部存储中加载图片时,有时会出现滞后现象,但有时会出现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,
};
` }
}
我想我需要处理它,但我不知道如何。
答案 0 :(得分:0)
您很可能永远不会在图像实例上调用image.Dispose()
方法,因此永远不会释放分配给图像的内存。此外,您应该考虑不加载所有文件,而是按需加载它们。
答案 1 :(得分:0)
我认为你应该在循环之外声明你的变量并重用它们,而不是在每次迭代时声明新的变量:
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,
};
}