Take the 2-minute tour ×

I am really trying to optimize my code for moving UIImageViews.

I have a primary method for enumerating through all of my UIImageViews.

Which enumeration would perform faster, or would they both perform exactly the same?

Example 1: In this example I enumerate through all UIImageViews in my UIViewController's view

 for (UIImageView *imageView in self.view.subviews)

Example 2: In this example I enumerate through all UIImageViews in my NSMutableArray. The array always contains all of the UIImageViews found in self.view.subviews (It's just a copy of all the UIImageViews normally found in self.view.subviews that I only need to create once)

 for (UIImageView *imageView in self.imageViews)

So which example should provide me with a faster and more optimized enumeration? Note that these are constantly enumerated by a CADisplayLink that I have setup. Also note that self.view.subviews returns an NSArray so perhaps this NSArray is constantly being created when I use the first enumeration, making me think that the NSMutableArray approach would perform better and have less overhead but i'm not sure.

Edit: All of the subviews are UIImageViews. I am also not sure if there is any significant overhead when using the subviews property because it looks like it may do some "behind-the-scenes" work to get me that NSArray.

share|improve this question
Are all the subviews uiimageview? –  lu yuan Mar 13 '13 at 3:29
Yes, assume all of the subviews are UIImageViews. –  Epic Byte Mar 13 '13 at 3:45
I prefer the first one to save more code:) –  lu yuan Mar 13 '13 at 4:00
Are you trying to solve a performance problem? Did you measure significant overhead? If not, stop now and read up on premature optimization. –  Jim Puls Mar 13 '13 at 4:05
If you're worried about efficiency, I'm surprised you're having them "constantly enumerated by a CADisplayLink". I would have thought that the impact of that would be far greater than NSMutableArray v subviews impact. –  Rob Mar 13 '13 at 4:10

2 Answers 2

up vote 2 down vote accepted

Instead of iterating through the subviews, you can use the Tag property to do it very fast


Add a tag to UIimageView through code or xib

myImageView.tag = 1;


 UIImageView *myImageView = ([[self.view viewWithTag:1] isKindOfClass:[UIImageView class]])?(UIImageView *)[self.view viewWithTag:1]:nil;

About your iteration codes

Assume all of the subviews are UIImageViews then the first code will be the most efficient.

for (UIImageView *imageView in self.view.subviews)
share|improve this answer
I don't know how I wound up downvoting this. Allow me to remedy that. +1 –  CodaFi Mar 13 '13 at 15:23
After doing some benchmarking, it appears that there is no significant difference in performance between the two examples. I ended up going with the subviews enumeration because I don't need to worry about constantly keeping the NSMutableArray updated. I also looked at the documentation for the subviews property and it looks to be safe to enumerate because it is just a copy. As for the CADisplayLink, I use it to constantly integrate the motion (and a few other mechanics) of my UIImageViews. –  Epic Byte Mar 13 '13 at 19:02

If you're dealing with a simple view with a couple of imageviews on it, it probably doesn't matter too much, and the performance issues are negligible. Benchmark the two approaches in Instruments, and I'd be shocked if there is an observable difference. And as I alluded to above, your use of a CADisplayLink can have far greater performance repercussions than a NSMutableArray ever will.

Conceptually, though, as your app grows in complexity, there are huge benefits in following the MVC pattern. In this world, there is a huge difference between the model (which might be backed by something as simple as your mutable array or as complicated as some persistent store or some network service) and the view (which, once you optimize it), may only represent a very small subset of the the whole model, employing lazy loading or dequeueing and reusing cells/views.

A wonderful example is a photo gallery app where you have lots of photo albums and each album has lots of photos. It would be crazy to try to load all of those images into image views if, for example, many of those image views are not visible at any given time, much less even load have separate image views for all of those images. So your model might be some object structure that captures the full set of albums and references to the photos, but your view might only have a few image views on it (e.g. the "current" image, the "next" image, and the "previous" image).

In a scenario like this, you'd be asking for a world of trouble if you start trying to load all of the images into image views and adding all of those image views as subviews. And even if you do, as you get more sophisticated, you may find yourself loading the imageviews with renditions of images that are down-sampled to a resolution appropriate for your device or what have you.

So, in short, looking at your simple example, I think there is a negligible performance hit in creating your NSMutableArray, but it leads you down the road of correctly thinking about the differences between your model (the data backing your app) and the view (the stuff you do to present and optimize your user interface). It probably doesn't matter for tiny little apps, but as your app gets more sophisticated (lots of images, user interface where you have some images that are not visible (scrolled off the screen), then this distinction becomes far more important.

We don't know enough about your app to be able to say where you fall on that spectrum of complexity. Sounds like it's simple enough that it might not matter, but I think it's good to adopt MVC thought processes sooner rather than later.


share|improve this answer

Your Answer


