2

I am trying to programatically create a series of background images that fade from one to another.

I am having trouble with memory as it doesn't seem as though I am successfully removing the UIImageView that I'm creating after their fade out animation is complete.

In my background controller swift file I have the following:

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

}

I also have a similar function to createBackgroundImage() that is called createInvisibleBackgroundImage() which does the same thing but instead gives the UIImageView an alpha property of 0 before attaching it to the Superview.

In my ViewController.swift file I have

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)

I am noticing that even though i have said remove bg1 from the superview after the fade in of bg2 is complete the memory usage is unchanged (49mb)

How do I remove bg1 from my memory so that I can in cycle through more images without them stacking up?

Pratik Shah
  • 563
  • 4
  • 14
Mikey Musch
  • 609
  • 3
  • 8
  • 21
  • The images are loaded and held in memory all the time by putting the images into the `imagesArray`. – dasdom Feb 26 '16 at 08:23
  • 1
    You can do this with CoreAnimation using a single image view - http://stackoverflow.com/questions/1550206/how-to-crossfade-between-2-images-on-iphone-using-core-animation – Paulw11 Feb 26 '16 at 08:39
  • Thanks Paulw11 but that solution is written in Objective-C. It at least gives me some direction when I try and figure out how to translate it – Mikey Musch Feb 27 '16 at 03:27
  • @dasdom I don't think this is the case. when I have `imagesArray` full of `UIImage` objects I am only using 3mb memory. However when I use `createBackgroundImage()` on all of those `UIImage` objects I am using 180mb of memory. Also, I tried adding `imagesArray.removeFirst()` and saw no difference in memory usage. – Mikey Musch Feb 27 '16 at 11:00

1 Answers1

1

Turns out using UIImage(named: String) permanently caches the image!

Instead I used UIImage(contentsOfFile: String) and removed all references to the UIImage which then worked!

See UIImage using contentsOfFile For more information!

Community
  • 1
  • 1
Mikey Musch
  • 609
  • 3
  • 8
  • 21