ios - 释放 UINavigationController 后内存仍然很高

标签 ios swift memory-leaks uinavigationcontroller uiimage

我在我的应用程序中使用了 rootVC“VC1”的 UINavigationController,VC1 包含一个 Collection View ,每个单元格内有 2 个图像。当用户选择单元格时,导航 Controller 会将图像从单元格传递到新的 vc“VC2”,然后将其推送到导航 Controller 的顶部。我的问题是,当我通过 popviewcontroller 取下 VC2 时,VC2 被正确释放,但内存保持在相同的较高级别(推送新 vc 后,它从 60mb 增加到 130mb)。我尝试将 image 设置为 nil,也将 imageview 设置为 nil,但这些都不起作用。这是我的一些代码:

class VC1: UIViewController {
 var selectedUserPollDetails : VC2?

func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell = collectionView.cellForItem(at: indexPath) as! AppUserCell

    selectedUserPollDetails = VC2()

    selectedUserPollDetails?.leftPhoto = cell.leftImageNode.image
        selectedUserPollDetails?.rightPhoto = cell.rightImageNode.image
        navigationController?.pushViewController(selectedUserPollDetails !, animated: true)
}
}

class VC2: UIViewController {

lazy var arrow : ArrowBack = {
    let arrow = ArrowBack()
    return arrow
}()

weak var leftPhoto: UIImage?
weak var rightPhoto: UIImage?

 var leftPhotoImageview: UIImageView = {
    let imageview = UIImageView()
    imageview.contentMode = .scaleAspectFill
    imageview.layer.cornerRadius = 5
    imageview.layer.masksToBounds = true
    return imageview
}()

 var rightPhotoImageview: UIImageView = {
    let imageview = UIImageView()
    imageview.contentMode = .scaleAspectFit
    imageview.layer.cornerRadius = 5
    imageview.clipsToBounds = true
    return imageview
}()

override func viewDidLoad() {
    super.viewDidLoad()
view.addSubview(leftPhotoImageview)
    view.addSubview(rightPhotoImageview)
  view.addSubview(arrow)
    arrow.addTarget(self, action: #selector(handleArrowBack), for: .touchUpInside)

}

func handleArrowBack(){
    navigationController?.popViewController(animated: true)
}

override func viewDidLayoutSubviews() {
    super.viewDidLayoutSubviews()

    leftPhotoImageview.frame = CGRect(x: 100, y: 0, width: 100, height: 100)
    rightPhotoImageview.frame = CGRect(x: 100, y: 200, width: 100, height: 100)

    if leftPhoto != nil, rightPhoto != nil{
        leftPhotoImageview.image = leftPhoto
        rightPhotoImageview.image = rightPhoto
    }
}

deinit{
    leftPhoto = nil
    rightPhoto = nil
    leftPhotoImageview.image = nil
    rightPhotoImageview.image = nil
}

我什至在最后添加了 deinit 以确保照片被释放。所以基本上当我尝试再次推送 VC2(弹出后)内存量再次翻倍(260mb)等等......是什么导致了这个问题?我究竟做错了什么? 顺便提一句。我省略了不太重要的函数和变量

最佳答案

你肯定有内存泄漏。我每次都相信 您通过导航 Controller 推送新 View ,它会创建一个全新的 View ,即该 View 是全新的并且不会重复使用。如果您在推送的 View 中有强引用,除非您取消初始化,否则它们不会被释放,因为它们对您推送的 View 有强引用,因此它们会徘徊。您提到您取消了这些项目。您是否也尝试过将 leftPhotoImageView 和 rightPhotoImageView 也标记为弱属性?似乎有什么东西保留着这些图像。

如果有意义的话,您还可以将 deinit 更改为 leftPhotoImageview = nil 和 rightPhotoImageView = nil,而不是将 imageview.image 属性设置为 nil。

关于ios - 释放 UINavigationController 后内存仍然很高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43053146/

相关文章:

ios - 如何从 Google GMS 地址获取 Google 地点 ID

ios - Firebase 能否可靠地用于检测可达性而不是 iOS 可达性 API

swift - 从协议(protocol)创建对象时,编译器没有引发错误

c++ - 返回指针(数组)时可能发生内存泄漏 (C++)

JavaScript:2011 年我应该担心内存泄漏吗?

ios - Xcode 6.4 - 无法对 View 或任何内容设置约束

ios - 添加 UIGestureRecognizer 以从左向右滑动我的 View

ios - GestureRecognizer 接受所有触摸输入

ios - swift 中的静态字典类型变量声明?

java - 何时以及如何将 java 类加载器标记为垃圾收集?