【问题标题】:Memory stays high after deallocating UINavigationController释放 UINavigationController 后内存保持高位
【发布时间】:2017-08-20 13:24:12
【问题描述】:

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

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)等等......是什么导致了这个问题?我究竟做错了什么? 顺便提一句。我省略了不太重要的 func 和 vars

【问题讨论】:

    标签: ios swift memory-leaks uinavigationcontroller uiimage


    【解决方案1】:

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

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

    【讨论】:

    • 每次当我将 imageview 投射为弱或无主时都会发生崩溃并且损坏的行在 viewdidload 'view,addSubview(leftPhotoImageview!)' 内,所以当我分配 VC2 imageview 时似乎是nil,因为 ARC 删除了对这个 imageview 的引用(?),但是为什么在 init 上不是 deinit,你可能知道为什么会这样吗?或任何线索?除此之外,我想我已经尝试了一切,这就是为什么最终来到这里的原因:(
    【解决方案2】:

    好的,我想我找到了解决方案,我不知道为什么我没有尝试这个,所以我将我的 imageview 标记为惰性,现在弹出 vc 后内存正在减少

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-22
      • 2021-01-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-21
      相关资源
      最近更新 更多