9

In my application I needed something like a particle system so I did the following:

While the application initializes I load a UIImage

laserImage = [UIImage imageNamed:@"laser.png"];

UIImage *laserImage is declared in the Interface of my Controller. Now every time I need a new particle this code makes one:

// add new Laserimage
UIImageView *newLaser = [[UIImageView alloc] initWithImage:laserImage];
[newLaser setTag:[model.lasers count]-9];
[newLaser setBounds:CGRectMake(0, 0, 17, 1)];
[newLaser setOpaque:YES];
[self.view addSubview:newLaser];
[newLaser release];

Please notice that the images are only 17px * 1px small and model.lasers is a internal array to do all the calculating seperated from graphical output. So in my main drawing loop I set all the UIImageView's positions to the calculated positions in my model.lasers array:

for (int i = 0; i < [model.lasers count]; i++) {
    [[self.view viewWithTag:i+10] setCenter:[[model.lasers objectAtIndex:i] pos]];
}

I incremented the tags by 10 because the default is 0 and I don't want to move all the views with the default tag.

So the animation looks fine with about 10 - 20 images but really gets slow when working with about 60 images. So my question is: Is there any way to optimize this without starting over in OpenGl ES?

4 Answers 4

8

As jeff7 and FenderMostro said, you're using the high-level API (UIKit), and you'd have better performance using the lower APIs, either CoreAnimation or OpenGL. (cocos2d is built on top of OpenGL)

  • Your best option would be to use CALayers instead of UIImageViews, get a CGImageRef from your UIImage and set it as the contents for these layers.

  • Also, you might want to keep a pool of CALayers and reuse them by hiding/showing as necessary. 60 CALayers of 17*1 pixels is not much, I've been doing it with hundreds of them without needing extra optimization.

This way, the images will already be decompressed and available in video memory. When using UIKit, everything goes through the CPU, not to mention the creation of UIViews which are pretty heavy objects.

1
  • This is the correct answer. Your code will work just fine as long as your use CALayers and have each of the use the same layer.contents set to the same CG image. Rewiring your whole game in OpenGL would be overkill, you just need to not allocate tons of UIImageView, that is the root of your implementation issue. See developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Conceptual/…
    – MoDJ
    Jul 1, 2013 at 8:34
3

Seems like you're trying to code a game by using the UIKit API, which is not really very suitable for this kind of purpose. You are expending the device's resources whenever you allocate a UIView, which incurs slowdowns because object creation is costly. You might be able to obtain the performance you want by dropping to CoreAnimation though, which is really good at drawing hundreds of images in a limited time frame, although it would still be much better if you used OpenGL or an engine like Cocos2d.

1

The UIImageView is made to display single OR multiple images. So, instead of creating every time a UIImageView, you should consider creating a new image and add it to the UIImageView instead.

See here.

6
  • Apple's documentation says UIImageView is for showing a single image or animating a series of images. Since I need all the images to be shown at once I cannot use this functionality... Other ideas?
    – tfeldmann
    Mar 24, 2010 at 13:41
  • ohh, sorry Thomas, I don't know where to proceed. Let's wait if there are some other answers from the community.
    – Hoang Pham
    Mar 24, 2010 at 13:59
  • Hi Thomas, the problem might be you have not cleared the drawing context. Can you double check that?
    – Hoang Pham
    Mar 24, 2010 at 22:05
  • Which context do you mean? Should I enable "Clears Context Before Drawing" for all my images? The parent UIView? Thank you so much for your help!
    – tfeldmann
    Mar 25, 2010 at 10:25
  • you have not shown us how you do the animation, so I just guess :-s Try this one if it helps: CGContextClearRect(UIGraphicsGetCurrentContext(), rect);
    – Hoang Pham
    Mar 25, 2010 at 10:39
0

I'd recommend starting over using OpenGL ES, there is an excellent framework called cocos2d for iPhone that can make this type of programming very easy and fast. From a quick look at your code, you're lasers can be remodeled as CCSprite which is an easy way to move images around a scene among many other things.

1
  • I just took a quick look at cocos2d and it seemed quite capable but I nearly finished my app and a rewrite would just cost too much time. Have you got any other ideas?
    – tfeldmann
    Mar 24, 2010 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.