亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從本地存儲加載圖片 > OutofmemoryException

從本地存儲加載圖片 > OutofmemoryException

C#
慕少森 2021-06-29 13:02:14
當我想從內部存儲加載圖片時,有時會延遲,但有時會出現 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()在圖像實例上調用方法,因此永遠不會釋放分配給圖像的內存。此外,您應該考慮不加載所有文件,而是按需加載它們。


查看完整回答
反對 回復 2021-07-03
?
富國滬深

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,

    };

}


查看完整回答
反對 回復 2021-07-03
  • 2 回答
  • 0 關注
  • 235 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號