0

I'm trying to write multiple images to files in a loop, so to do that, i loop over all my images, get the jpegData and then write it into files.

Pretty straight forward.

But then i noticed something: images are never released until the loop is completely over. So i ran this code to test it :

    for _ in 0 ... 1000
    {
        var image: UIImage? = nil
        var jpegData: Data? = nil
        image = UIImage( named: "Texture" )
        jpegData = image!.jpegData( compressionQuality: 0.5 )
        image = nil
        jpegData = nil
    }

All that does is: it reads a 800x800 texture from resource, and then gets the jpegData from it. It doesn't do anything else.

I just run this, and i see my memory usage climbing up, up until the for loop is over, then it gets back down.

My question is : How does the retain life cycle of a UIImage work in this situation, and how can i release them from memory in this case ?

My problem is : I can't save many images like this, because it'll crash my app because of memory overflow before the loop if over.

D4mN
  • 31
  • 4

0 Answers0