Ios .removeFromSuperview不使用UIImageView

Ios .removeFromSuperview不使用UIImageView,ios,xcode,memory,uiimageview,swift2,Ios,Xcode,Memory,Uiimageview,Swift2,我试图通过编程方式创建一系列背景图像,这些图像会从一个背景图像淡入另一个背景图像 我的内存有问题,因为在它们的淡出动画完成后,我似乎没有成功删除我正在创建的UIImageView 在我的后台控制器swift文件中,我有以下内容: let URL1 = "bg_1.jpg" let img1 = UIImage(named: URL1) let URL2 = "bg_2.jpg" let img2 = UIImage(named: URL2) let imagesArray: [UIImage]

我试图通过编程方式创建一系列背景图像,这些图像会从一个背景图像淡入另一个背景图像

我的内存有问题,因为在它们的淡出动画完成后,我似乎没有成功删除我正在创建的
UIImageView

在我的后台控制器swift文件中,我有以下内容:

let URL1 = "bg_1.jpg"
let img1 = UIImage(named: URL1)
let URL2 = "bg_2.jpg"
let img2 = UIImage(named: URL2)

let imagesArray: [UIImage] = [
    img1!,
    img2!,
]

var backgroundImageArray: [UIImageView] = []

func createBackgroundImage(view: UIView, backgroundImage: UIImage) -> UIImageView {

    let backgroundImageView = UIImageView(image: backgroundImage)
    backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
    view.addSubview(backgroundImageView)

    let horizontalConstraint = NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
    view.addConstraint(horizontalConstraint)

    let verticalConstraint = NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
    view.addConstraint(verticalConstraint)

    let widthConstraint = NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0)
    view.addConstraint(widthConstraint)

    let heightConstraint = NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0)
    view.addConstraint(heightConstraint)

    backgroundImageView.contentMode = .ScaleAspectFill

    return backgroundImageView

}
我还有一个类似于
createBackgroundImage()
的函数,名为
createInvisibleBackgroundImage()
,它做了同样的事情,但是在将
UIImageView
附加到Superview之前,它会给它一个alpha属性0

在我的
ViewController.swift
文件中

func animateBackgroundImages(iteration: Int, mainView: UIView) {       

if iteration == 0 {
            var bg1: UIImageView = createBackgroundImage(mainView, backgroundImage: imagesArray[0])
            var bg2: UIImageView = createInvisibleBackgroundImage(mainView, backgroundImage: imagesArray[1])

        UIView.animateWithDuration(5.0, delay: 1.0, options: UIViewAnimationOptions.TransitionNone, animations: {
            bg2.alpha = 1
            }, completion: {
                (Bool) in
                bg1.removeFromSuperview()
        })

    }

    if iteration > 0 && iteration < 7 {
        // This is for later when I get it working
    }

}

animateBackgroundImages(0, mainView: view)
func animateBackgroundImages(迭代:Int,主视图:UIView){
如果迭代==0{
变量bg1:UIImageView=createBackgroundImage(主视图,背景图像:imagesArray[0])
变量bg2:UIImageView=createInvisibleBackgroundImage(主视图,背景图像:imagesArray[1])
UIView.animateWithDuration(5.0,延迟:1.0,选项:UIViewAnimationOptions.TransitionNone,动画:{
bg2.alpha=1
},完成日期:{
(Bool)在
bg1.removeFromSuperview()的
})
}
如果迭代>0&&迭代<7{
//这是我以后用的
}
}
动画背景图像(0,主视图:视图)
我注意到,尽管我说过在bg2淡入完成后从superview中删除bg1,但内存使用量没有变化(49mb)


如何从内存中删除bg1,以便循环浏览更多图像而不使它们堆积起来?

结果是使用
UIImage(命名:String)
永久缓存图像

相反,我使用了
UIImage(contentsOfFile:String)
并删除了对UIImage的所有引用,这样就可以工作了


有关更多信息,请参阅

通过将图像放入
imagesArray
,图像一直被加载并保存在内存中。您可以使用单个图像视图通过CoreAnimation实现这一点-谢谢Paulw11,但该解决方案是用Objective-C编写的。当我试着找出如何翻译时,它至少给了我一些指导it@dasdom我不认为这是最好的案例当我有
imagesArray
充满
UIImage
对象时,我只使用3mb内存。然而,当我在所有这些
UIImage
对象上使用
createBackgroundImage()
时,我使用的是180mb内存。此外,我还尝试添加了
imagesArray.removeFirst()
,并没有发现内存使用方面的差异。