iphone - classes, objects and memory consumption
Suppose I have an application that will draw 20 little UIImageViews on the screen. Suppose 20 playing cards faced dow开发者_StackOverflown. So, you have the same image 20 times on the screen.
Suppose I have two cases:
CASE 1
I have a method not on a class that loads the card back texture, applies to a UIImageView and positions it on the screen. This method is run 20 times, so all cards are put on screen as image views.
CASE 2
I have a class that once initialized will load the card background image, apply to a UIImageView and deliver it as an object.
Suppose that now I want to animate the 20 cards, using simple UIImageView translation.
Looking by the point of view of memory consumption, CPU load and run time execution which of the two cases I mentioned will be lighter for the application and consume less memory?
thanks.
Almost certainly doesn't matter. The real constraint is probably views and GPU memory, which will be the same because you're still creating the same 20 UIImageViews. Your setup cost is almost certainly minuscule in either case in any of the categories you mention. And 20 images is probably no problem either. Make sure the images are sized reasonably. And load them with UIImage's +imageNamed:
method to take advantage of caching in the frameworks.
You can profile with Instruments (via Xcode) if you'd like to actually determine specific performance differences.
From my understanding, case 2 will consume less memory, but it won't be so much of a difference.
Can you confirm that in case 2 you will only have one UIImageView in your window? But if that's the case then it'll be harder to flip the cards using the second method.
精彩评论