views:

306

answers:

4

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?

Thank you very much and sorry for my english! Greetings from Germany, Thomas

A: 

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.

jeff7
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?
Thomas
A: 

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.

sfa
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?
Thomas
ohh, sorry Thomas, I don't know where to proceed. Let's wait if there are some other answers from the community.
sfa
Hi Thomas, the problem might be you have not cleared the drawing context. Can you double check that?
sfa
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!
Thomas
you have not shown us how you do the animation, so I just guess :-s Try this one if it helps: CGContextClearRect(UIGraphicsGetCurrentContext(), rect);
sfa
Thanks for your try! I'm not using a costum drawRect method - I just do a setCenter: every frame. Next thing I'll try is whether it is faster to move all those UIImageViews or to have a single UIImageView in the background and draw all the small images into it. Might need to clear the context then ;-)
Thomas
A: 

Hello, 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.

FenderMostro
+1  A: 

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.

nico_nico