【发布时间】: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