On one hand, I’m just curious.
On the other, I have to count somehow the amount of images created (to trigger cleanup at some quantity). Unfortunately memory warnings notify me too late, the app gets terminated before I have any chance to react, so thatswhy I want to work this issue around.
I could scatter around my project with a global counter, that increments before I make any UIImage operation, but obviously this is exactly what I want to avoid, that would result in an unmaintainable design.
So I was thinking about subclass UIImage initializers (some kind of MYImage class), than use MYImage troughout the project, but UIImageView’s would use UIImages anyway (maybe I can set UIImageView’s setImage method, but I bet UIKit uses actual _image instance variable at many cases).
The best would be to count pixels amount from UIImage sizes, so I could flush image memory above a given pixelcount.
Any notification? Something trough categories? Or some kind of NSObject KVO observing (with literal checkings on class/method names)? Do you have ideas/experiences?
There a a lot of ways you could accomplish this. One way would be to override one (or more) of the image creation methods in a subclass, get the size of the image created, and add that to a user default to keep track of the cumulative sizes of all the images. For example, I overrode imageNamed like this:
In the applicationDidFinishLaunchingWithOptions: method, I set the value of that key to zero.
In the view controller when I create an image, I just use my class to make a new one:
Another way would be to override the setImage: method of UIImageView, and do the same kind of thing inside that method.