問題
開発中の iOS アプリにおいて、写真を大量に表示した際にメモリ枯渇によりアプリが突然終了してしまう問題に直面した。
Simulator 上で問題を再現させた際には、以下のエラーメッセージが出力された。
Domain: IDEDebugSessionErrorDomain
Code: 4
Failure Reason: Message from debugger: Terminated due to memory issue
User Info: {
IDERunOperationFailingWorker = DBGLLDBLauncher;
}
具体的には、以下のように SwiftUI の LazyVGrid
を使って画像を大量に表示させた際にこの問題は発生した。
LazyVGrid(columns: self.columns, alignment: .center, spacing: 0.0) {
ForEach(entries, id:\.self) { entry in
EntryView(entry: entry)
}
}
解決方法
表示させる画像の解像度を下げることで、この問題を解消することができた。
具体的には、以下のページで紹介されている downsampling の処理を利用した。
https://swiftsenpai.com/development/reduce-uiimage-memory-footprint/