gpt4 book ai didi

arrays - 来自调试器的消息 : Terminated due to memory issue in Xcode 10. 1...图像数组?

转载 作者:可可西里 更新时间:2023-11-01 00:27:46 27 4
gpt4 key购买 nike

我是一名新开发者,我的应用有一个包含 32 张图片的 Collection View (水平滚动)。我已经为图像、标签和文本创建了数组。 当我到达列表末尾时,应用程序崩溃了。无论我的图像大小如何,我都会收到“来自调试器的消息:由于内存问题而终止”。我使用了 Instruments 工具,这似乎表明图像没有被释放。

我尝试调整图像大小但没有成功。我也尝试过使用 autoreleasepool,我可能使用不当。

我的图像数组是这样设置的,图像 1-32。

let imageArray = [UIImage (named: "1"), UIImage (named: "2"), etc.]

这是我的收藏 View :

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.imageArray.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MainCollectionViewCell", for: indexPath) as! MainCollectionViewCell

cell.imgImage.image = imageArray [indexPath.row]
cell.lblImageName.text = nameArray [indexPath.row]

return cell
}

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {

let mainStoryboard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
let desVC = mainStoryboard.instantiateViewController(withIdentifier: "DetailViewController") as! DetailViewController


desVC.image = imageArray[indexPath.row]!
desVC.name = nameArray[indexPath.row]
desVC.text = textArray[indexPath.row]

self.navigationController?.pushViewController(desVC, animated: true)
}

最佳答案

一些观察:

  1. 不要预先加载图像。根据需要加载它们。

    因此,与其保存 UIImage 数组的 imageArray,不如保存图像名称数组,例如

    let imageNames = ["1", "2", ...]

    然后,在 cellForItemAt 中,您可以在此处为该特定单元格实例化 UIImage,例如

    cell.imgImage.image = UIImage(named: imageNames[indexPath.row])
  2. 请记住,图像会占用大量内存。无论 JPG 或 PNG 的大小如何,当它们在 UI 中使用时,它们通常每个像素使用 4 个字节。所以考虑一张 2000×2000 像素的图像,它的 JPG 可能只有 100kb:即使在一个很小的 ​​100×100 ImageView 中使用,整个图像也是未压缩的,在这种情况下,会用掉 16mb!因此,请确保您的图像具有适合您的 UI 的尺寸。例如。如果你的 ImageView 是 100×100,那么你可能有三种图像再现,一种是 100×100,用于非视网膜设备,一种是 200×200,用于@2x 视网膜设备,一种是 300× @3x 设备为 300。

  3. 关于内存没有释放,三点:

    • 消除您对 UIImage 数组的使用,这将避免强制应用将所有图像保存在内存中,即使在任何给定时刻可能并不需要它们(请参阅第 1 点,上文)。

    • 确保您没有强大的引用循环。因此,关闭有问题的 View Controller ,然后使用“调试内存图”并确保 View Controller 确实被关闭了。

    • 当您使用 UIImage(named:) 时,图像将被缓存并且内存不会被释放,直到出现内存压力(例如内存警告)。通常 UIImage(named:) 缓存行为会起作用(当您修复上述问题时),但如果不起作用,请使用 UIImage(contentsOfFile:)反而。正如 UIImage(named:) 文档警告我们的那样:

      This method looks in the system caches for an image object with the specified name and returns the variant of that image that is best suited for the main screen. If a matching image object is not already in the cache, this method locates and loads the image data from disk or from an available asset catalog, and then returns the resulting object.

      The system may purge cached image data at any time to free up memory. Purging occurs only for images that are in the cache but are not currently being used ...

      If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.

关于arrays - 来自调试器的消息 : Terminated due to memory issue in Xcode 10. 1...图像数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55344471/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com