當我想從內部存儲加載圖片時,有時會延遲,但有時會出現 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, }; ` } }我想我需要處理它,但我不知道如何處理。
2 回答

明月笑刀無情
TA貢獻1828條經驗 獲得超4個贊
很可能您永遠不會image.Dispose()
在圖像實例上調用方法,因此永遠不會釋放分配給圖像的內存。此外,您應該考慮不加載所有文件,而是按需加載它們。

富國滬深
TA貢獻1790條經驗 獲得超9個贊
我認為您應該在循環外聲明變量并重用它們,而不是在每次迭代時聲明新變量:
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,
};
}
- 2 回答
- 0 關注
- 235 瀏覽
添加回答
舉報
0/150
提交
取消