DismissViewController不使用缓存的图像释放内存。迅速

时间:2016-10-11 01:29:36

标签: ios swift memory-management uiimageview

我的问题标题可能会留下一些不足之处,但这是基本问题:

我提出了UIViewController,其中添加了UIScrollView。在滚动视图中,我使用UIImageView来布局大量预先加载的图像,其中图像本身是从我的图像资源缓存的。图像非常大,因为它们的质量非常好。当呈现UIViewController时,会分配大量内存来布局图像,但是当使用dismissViewControllerAnimated解散VC时,VC中的所有内存似乎都被解除分配,除了来自图像(实际上是大部分使用的内存)。

我尝试将图像存储在可选图像视图的数组中,如:[UIImageView?],然后在调用VC被解除之前,调用removeFromSuperview每一个,然后设置为nil

问题是无论我做什么,内存永远不会被释放。当UIViewController被解除时,如何确保正确释放用于这些图像的内存?

以下是UIImageView的布局方式:

    for i in 0 ..< preLoadedImageNames.count {

        let contentWidth:CGFloat = min(self.view.bounds.width, self.view.bounds.height)/3
        let contentHeight:CGFloat = height

        let content:UIView = UIView()
        scroll.addSubview(content)
        content.translatesAutoresizingMaskIntoConstraints = false
        let contentHeightConstraint:NSLayoutConstraint = NSLayoutConstraint(item: content, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: contentHeight)
        let contentWidthConstraint:NSLayoutConstraint = NSLayoutConstraint(item: content, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: contentWidth)
        let contentTop:NSLayoutConstraint = NSLayoutConstraint(item: content, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: scroll, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0)
        let contentLeft:NSLayoutConstraint = NSLayoutConstraint(item: content, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: scroll, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: contentWidth*CGFloat(i))
        scroll.addConstraints([contentHeightConstraint, contentWidthConstraint, contentTop, contentLeft])

        let imageHeight:CGFloat = contentHeight*0.90
        let imageWidth:CGFloat = contentWidth*0.90

        let thumbnail:UIImageView = UIImageView()
        content.addSubview(thumbnail)
        thumbnail.image = UIImage(named: preLoadedImageNames[i])
        thumbnail.contentMode = UIViewContentMode.ScaleAspectFit
        thumbnail.frame = CGRect(x: contentWidth/2 - imageWidth/2, y: contentHeight/2 - imageHeight/2, width: imageWidth, height: imageHeight)

        images.append(thumbnail)

        let tap:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(ViewController.quickStartTapped(_:)))
        content.addGestureRecognizer(tap)
        self.quickStartTaps.append(tap)

        scroll.contentSize.width += contentWidth

    }

我在这里做错了吗?为什么在取消视图控制器时,用于布局这些图像的内存未被释放?

我感谢任何帮助。

0 个答案:

没有答案