解决了从呈现的UITableViews中增加内存的问题

问题描述 投票:0回答:1

我的应用程序的问题是内存使用率不断上升。我有一个使用多个UITableView的应用程序,其设置如下:

  • UITableView 1进入UITableView 2

  • UITableView 1进入UITableView 3

  • UITableView 1进入UITableView 4

  • UITableView 1进入UITableView 5

  • UITableView 1进入UITableView 6

UITableView 2、3、4、5和6中的所有单元格都包含图像。当我输入UITableView 2、3、4、5和6时,随着应用程序加载图像,内存使用量将按预期增加。然后,当我解散我所在的UITableview并输入另一个UITableview时,内存使用率继续上升。

我想做的是在关闭UITableview 2、3、4、5或6时清除内存。这将意味着内存使用率不会持续增加。

我目前正在使用以下方法关闭UITableView:

@IBAction func Back(_ sender: Any) {

    NotificationCenter.default.removeObserver(self)

    self.dismiss(animated: true, completion: nil)
}

图像存储在应用程序的文件夹中。图像的名称存储在一个数组中,在本例中,此数组称为foodImageArray。通过读取数组并将每个单元格链接到其对应的图像,UItableView图像按顺序填充。

cell.foodImage.image = UIImage(named: foodImageArray[indexPath.row] + ".jpg")
ios swift uitableview memory-management memcached
1个回答
0
投票

您遇到的问题可能是因为imageNamed:方法缓存了图像,这会使您的内存占用量增加。

尝试此以加载图像:

let path = Bundle.main.path(forResource: image, ofType: nil)! 
let original = UIImage(contentsOfFile: path)!
© www.soinside.com 2019 - 2024. All rights reserved.