1

My code shows weird behavior, and I've searched for hours with no luck.

My code snippet:

func loadImageFromLocal(index:String)->UIImage{
        var fileManager = NSFileManager.defaultManager()
        var containerURL = fileManager.containerURLForSecurityApplicationGroupIdentifier("group.mygroup.test")
        var pngNumber=sharedDefaults.objectForKey("png\(index)") as Int
        var filePath = containerURL!.path!+"/img\(pngNumber).png"

        NSLog(filePath)

        var image:UIImage = UIImage(contentsOfFile: filePath)!
        return image
}

...

var image:UIImage?=loadImageFromLocal(button.titleForState(.Normal)!)
var pngImage = UIImagePNGRepresentation(image)

As you can see, I save image to local folder and when I need it, I load it from load image from local folder and convert it as NSData with UIImagePNGRepresentation

Weird thing is, for some image, it works as I expected. However, for some image, the memory won't deallocated. (Checked with Allocation instrument. The heap keeps going up)

second one is deallocated, first one is alive (see the dot representing it is alive)

See the dot meaning the first one is still alive? I checked the history, and when it goes well, it should be like thisenter image description here

For the problematic image, it is like this enter image description here

Two alloc, one dealloc..I guess that is the problem but I don't know why it happens only for specific image. When I check the stack trace, I guess it means UIImagePNGRepresentation is the problem but I have no idea what this means exactly so I will attach the stack trace. enter image description here

Any help will be appreciated!! Thanks!!

kwmaeng
  • 631
  • 2
  • 5
  • 20

0 Answers0